1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #![feature(file_create_new)]
17 use utils_rust::file_ex;
18 use std::fs::File;
19 use std::io::prelude::*;
20 use std::fs::remove_file;
21 use std::os::unix::io::{AsRawFd, RawFd};
22 use std::ffi::c_char;
23 
24 pub const CONTENT_STR: &str = "TTtt@#$%^&*()_+~`";
25 pub const FILE_PATH: &str = "./tmp.txt";
26 pub const NULL_STR: &str = "";
27 pub const MAX_FILE_LENGTH: usize = 32 * 1024 * 1024;
28 
29 // This code is converted from 43 functions of the utils_file_test.cpp.
30 
create_test_file(path: &String, content: &String) -> bool31 pub fn create_test_file(path: &String, content: &String) -> bool {
32     if let Ok(mut file) = File::create(path) {
33         if let Err(err) = file.write_all(content.as_bytes()) {
34             println!("Error writing to file: {}", err);
35             return false;
36         }
37         true
38     } else {
39         println!("Failed to create file: {}", path);
40         false
41     }
42 }
43 
remove_test_file(path: &String) -> Result<(), std::io::Error>44 pub fn remove_test_file(path: &String) -> Result<(), std::io::Error> {
45     remove_file(path)
46 }
47 
48 #[test]
test_load_string_from_file_001()49 fn test_load_string_from_file_001() {
50     let mut str = String::new();
51     let filename = String::from("/proc/meminfo");
52     assert!(file_ex::ffi::RustLoadStringFromFile(&filename, &mut str));
53 
54     let mut str2 = String::new();
55     let file = File::open(&filename).expect("Failed to open file");
56     let fd: RawFd = file.as_raw_fd();
57     assert!(file_ex::ffi::RustLoadStringFromFd(fd, &mut str2));
58     assert_eq!(str.len(), str2.len());
59 
60     let mut buff: Vec<c_char> = Vec::new();
61     let ret = file_ex::ffi::RustLoadBufferFromFile(&filename, &mut buff);
62     assert!(ret);
63     assert_eq!(str2.len(), buff.len());
64 }
65 
66 #[test]
test_load_string_from_file_002()67 fn test_load_string_from_file_002() {
68     let mut str = String::new();
69     let filename = NULL_STR.to_string();
70     assert!(!file_ex::ffi::RustLoadStringFromFile(&filename, &mut str));
71     assert!(str.is_empty());
72 }
73 
74 #[test]
test_load_string_from_file_003()75 fn test_load_string_from_file_003() {
76     let mut str = String::new();
77     let filename = FILE_PATH.to_string() + ".003";
78     let content = NULL_STR.to_string();
79     create_test_file(&filename, &content);
80     assert!(file_ex::ffi::RustLoadStringFromFile(&filename, &mut str));
81     let _err = remove_test_file(&filename);
82     assert_eq!(str, content);
83 }
84 
85 #[test]
test_load_string_from_file_004()86 fn test_load_string_from_file_004() {
87     let mut str = String::new();
88     let filename = FILE_PATH.to_string() + ".004";
89     let content = CONTENT_STR.to_string();
90     create_test_file(&filename, &content);
91     assert!(file_ex::ffi::RustLoadStringFromFile(&filename, &mut str));
92     let _err = remove_test_file(&filename);
93     assert_eq!(str, content);
94 }
95 
96 #[test]
test_load_string_from_file_005()97 fn test_load_string_from_file_005() {
98     let mut str = String::new();
99     let filename = FILE_PATH.to_string() + ".005";
100     let content: String = "t".repeat(MAX_FILE_LENGTH);
101     create_test_file(&filename, &content);
102     assert!(file_ex::ffi::RustLoadStringFromFile(&filename, &mut str));
103     let _err = remove_test_file(&filename);
104     assert_eq!(str, content);
105 }
106 
107 #[test]
test_load_string_from_file_006()108 fn test_load_string_from_file_006() {
109     let mut str = String::new();
110     let filename = FILE_PATH.to_string() + ".006";
111     let content: String = "t".repeat(MAX_FILE_LENGTH + 1);
112     create_test_file(&filename, &content);
113     assert!(!file_ex::ffi::RustLoadStringFromFile(&filename, &mut str));
114     let _err = remove_test_file(&filename);
115     assert!(str.is_empty());
116 }
117 
118 #[test]
test_load_string_from_fd_001()119 fn test_load_string_from_fd_001() {
120     let mut result = String::new();
121     assert!(!file_ex::ffi::RustLoadStringFromFd(-1, &mut result));
122     assert_eq!(result, "");
123 }
124 
125 #[test]
test_load_string_from_fd_002()126 fn test_load_string_from_fd_002() {
127     let mut result = String::new();
128     let filename =FILE_PATH.to_string() + ".008";
129     let content = NULL_STR.to_string();
130     create_test_file(&filename, &content);
131     let file = File::open(&filename).expect("Failed to open file");
132     let fd: RawFd = file.as_raw_fd();
133     assert!(file_ex::ffi::RustLoadStringFromFd(fd, &mut result));
134     let _err = remove_test_file(&filename);
135     assert_eq!(result, content);
136 }
137 
138 #[test]
test_load_string_from_fd_003()139 fn test_load_string_from_fd_003() {
140     let mut result = String::new();
141     let filename = FILE_PATH.to_string() + ".009";
142     let content = CONTENT_STR.to_string();
143     create_test_file(&filename, &content);
144     let file = File::open(&filename).expect("Failed to open file");
145     let fd: RawFd = file.as_raw_fd();
146     assert!(file_ex::ffi::RustLoadStringFromFd(fd, &mut result));
147     let _err = remove_test_file(&filename);
148     assert_eq!(result, content);
149 }
150 
151 #[test]
test_load_string_from_fd_004()152 fn test_load_string_from_fd_004() {
153     let mut result = String::new();
154     let filename = FILE_PATH.to_string() + ".010";
155     let content: String = "t".repeat(MAX_FILE_LENGTH);
156     create_test_file(&filename, &content);
157     let file = File::open(&filename).expect("Failed to open file");
158     let fd: RawFd = file.as_raw_fd();
159     assert!(file_ex::ffi::RustLoadStringFromFd(fd, &mut result));
160     let _err = remove_test_file(&filename);
161     assert_eq!(result, content);
162 }
163 
164 #[test]
test_load_string_from_fd_005()165 fn test_load_string_from_fd_005() {
166     let mut result = String::new();
167     let filename = FILE_PATH.to_string() + ".011";
168     let content: String = "t".repeat(MAX_FILE_LENGTH + 1);
169     create_test_file(&filename, &content);
170     let file = File::open(&filename).expect("Failed to open file");
171     let fd: RawFd = file.as_raw_fd();
172     assert!(!file_ex::ffi::RustLoadStringFromFd(fd, &mut result));
173     let _err = remove_test_file(&filename);
174     assert_ne!(result, content);
175 }
176 
177 #[test]
test_load_string_from_fd_006()178 fn test_load_string_from_fd_006() {
179     let mut result = String::new();
180     let filename = FILE_PATH.to_string() + ".012";
181     let content = CONTENT_STR.to_string();
182     create_test_file(&filename, &content);
183     let fd: i32;
184     {
185         let file = File::open(&filename).expect("Failed to open file");
186         fd = file.as_raw_fd();
187     }
188     assert!(!file_ex::ffi::RustLoadStringFromFd(fd, &mut result));
189     let _err = remove_test_file(&filename);
190     assert_eq!(result, "");
191 }
192 
193 #[test]
test_save_string_to_file_001()194 fn test_save_string_to_file_001() {
195     let path = FILE_PATH.to_string() + ".013";
196     let content = CONTENT_STR.to_string();
197     let new_content = NULL_STR.to_string();
198     create_test_file(&path, &content);
199     let ret = file_ex::ffi::RustSaveStringToFile(&path, &new_content, true);
200     assert!(ret);
201 
202     let mut load_result = String::new();
203     assert!(file_ex::ffi::RustLoadStringFromFile(&path, &mut load_result));
204     let _err = remove_test_file(&path);
205     assert_eq!(load_result, content);
206 }
207 
208 #[test]
test_save_string_to_file_002()209 fn test_save_string_to_file_002() {
210     let path = FILE_PATH.to_string() + ".014";
211     let content = "Before truncated!".to_string();
212     create_test_file(&path, &content);
213 
214     let new_content = CONTENT_STR.to_string();
215     assert!(file_ex::ffi::RustSaveStringToFile(&path, &new_content, true));
216 
217     let mut load_result = String::new();
218     assert!(file_ex::ffi::RustLoadStringFromFile(&path, &mut load_result));
219     let _err = remove_test_file(&path);
220     assert_eq!(load_result, new_content);
221 }
222 
223 #[test]
test_save_string_to_file_003()224 fn test_save_string_to_file_003() {
225     let path = FILE_PATH.to_string() + ".015";
226     let content = "Before truncated!".to_string();
227     create_test_file(&path, &content);
228 
229     let new_content = NULL_STR.to_string();
230     assert!(file_ex::ffi::RustSaveStringToFile(&path, &new_content, true));
231 
232     let mut load_result = String::new();
233     let ret = file_ex::ffi::RustLoadStringFromFile(&path, &mut load_result);
234     let _err = remove_test_file(&path);
235     assert!(ret);
236     assert_eq!(load_result, content);
237 }
238 
239 #[test]
test_save_string_to_file_004()240 fn test_save_string_to_file_004() {
241     let path = FILE_PATH.to_string()+ ".016";
242     let new_content = NULL_STR.to_string();
243     let content = "Before truncated!".to_string();
244     create_test_file(&path, &content);
245     assert!(file_ex::ffi::RustSaveStringToFile(&path, &new_content, false));
246 
247     let mut load_result = String::new();
248     let ret = file_ex::ffi::RustLoadStringFromFile(&path, &mut load_result);
249     let _err = remove_test_file(&path);
250     assert!(ret);
251     assert_eq!(load_result, content);
252 }
253 
254 #[test]
test_save_string_to_file_005()255 fn test_save_string_to_file_005() {
256     let path = FILE_PATH.to_string()+ ".017";
257     let content = "Before truncated!".to_string();
258     create_test_file(&path, &content);
259 
260     let new_content = CONTENT_STR.to_string();
261     assert!(file_ex::ffi::RustSaveStringToFile(&path, &new_content, false));
262 
263     let mut load_result = String::new();
264     let ret = file_ex::ffi::RustLoadStringFromFile(&path, &mut load_result);
265     let _err = remove_test_file(&path);
266     assert!(ret);
267     assert_eq!(load_result, content + &new_content);
268 }
269 
270 #[test]
test_save_string_to_fd_001()271 fn test_save_string_to_fd_001() {
272     let mut content = String::new();
273     let mut ret = file_ex::ffi::RustSaveStringToFd(0, &content);
274     assert!(!ret);
275     ret = file_ex::ffi::RustSaveStringToFd(-1, &content);
276     assert!(!ret);
277 
278     content = CONTENT_STR.to_string();
279     ret = file_ex::ffi::RustSaveStringToFd(0, &content);
280     assert!(!ret);
281     ret = file_ex::ffi::RustSaveStringToFd(-1, &content);
282     assert!(!ret);
283 }
284 
285 #[test]
test_save_string_to_fd_002()286 fn test_save_string_to_fd_002() {
287     let content = String::new();
288     let filename = FILE_PATH.to_string() + ".019";
289     let mut file = File::create_new(&filename).expect("Failed to create file");
290     let mut fd: RawFd = file.as_raw_fd();
291     let mut ret = file_ex::ffi::RustSaveStringToFd(fd, &content);
292     assert!(ret);
293 
294     let mut load_result = String::new();
295     file = File::open(&filename).expect("Failed to open file");
296     fd = file.as_raw_fd();
297     ret = file_ex::ffi::RustLoadStringFromFd(fd, &mut load_result);
298     let _err = remove_test_file(&filename);
299     assert!(ret);
300     assert_eq!(load_result, "");
301 }
302 
303 #[test]
test_save_string_to_fd_003()304 fn test_save_string_to_fd_003() {
305     let content = CONTENT_STR.to_string();
306     let filename = FILE_PATH.to_string() + ".020";
307     let mut file = File::create_new(&filename).expect("Failed to create file");
308     let mut fd: RawFd = file.as_raw_fd();
309     let mut ret = file_ex::ffi::RustSaveStringToFd(fd, &content);
310     assert!(ret);
311 
312     let mut load_result = String::new();
313     file = File::open(&filename).expect("Failed to open file");
314     fd = file.as_raw_fd();
315     ret = file_ex::ffi::RustLoadStringFromFd(fd, &mut load_result);
316     let _err = remove_test_file(&filename);
317     assert!(ret);
318     assert_eq!(load_result, content);
319 }
320 
321 #[test]
test_save_string_to_fd_004()322 fn test_save_string_to_fd_004() {
323     let content = CONTENT_STR.to_string();
324     let filename = FILE_PATH.to_string() + ".021";
325     File::create(&filename).expect("Failed to create file");
326     let mut file = File::open(&filename).expect("Failed to open file");
327     let mut fd: RawFd = file.as_raw_fd();
328     let mut ret = file_ex::ffi::RustSaveStringToFd(fd, &content);
329     assert!(!ret);
330 
331     let mut load_result = String::new();
332     file = File::open(&filename).expect("Failed to open file");
333     fd = file.as_raw_fd();
334     ret = file_ex::ffi::RustLoadStringFromFd(fd, &mut load_result);
335     let _err = remove_test_file(&filename);
336     assert!(ret);
337     assert_eq!(load_result, "");
338 }
339 
340 #[test]
test_load_buffer_from_file_001()341 fn test_load_buffer_from_file_001() {
342     let mut buff: Vec<c_char> = Vec::new();
343     let filename = FILE_PATH.to_string() + ".022";
344     let ret = file_ex::ffi::RustLoadBufferFromFile(&filename, &mut buff);
345     assert!(!ret);
346     assert_eq!(0, buff.len());
347 }
348 
349 #[test]
test_load_buffer_from_file_002()350 fn test_load_buffer_from_file_002() {
351     let mut buff: Vec<c_char> = Vec::new();
352     let filename = FILE_PATH.to_string() + ".023";
353     let content = "".to_string();
354     create_test_file(&filename, &content);
355     let ret = file_ex::ffi::RustLoadBufferFromFile(&filename, &mut buff);
356     let _err = remove_test_file(&filename);
357     assert!(ret);
358     assert_eq!(0, buff.len());
359 }
360 
361 #[test]
test_load_buffer_from_file_003()362 fn test_load_buffer_from_file_003() {
363     let mut buff: Vec<c_char> = Vec::new();
364     let filename = FILE_PATH.to_string() + ".024";
365     let content = "TXB".to_string();
366     create_test_file(&filename, &content);
367     let ret = file_ex::ffi::RustLoadBufferFromFile(&filename, &mut buff);
368     let _err = remove_test_file(&filename);
369     assert!(ret);
370     assert_eq!(3, buff.len());
371     assert_eq!('T' as c_char, buff[0]);
372     assert_eq!('X' as c_char, buff[1]);
373     assert_eq!('B' as c_char, buff[2]);
374 }
375 
376 #[test]
test_load_buffer_from_file_004()377 fn test_load_buffer_from_file_004() {
378     let mut buff: Vec<c_char> = Vec::new();
379     let filename = FILE_PATH.to_string() + ".025";
380     let content = "t".repeat(MAX_FILE_LENGTH + 1);
381     create_test_file(&filename, &content);
382     let ret = file_ex::ffi::RustLoadBufferFromFile(&filename, &mut buff);
383     let _err = remove_test_file(&filename);
384     assert!(!ret);
385     assert_eq!(0, buff.len());
386 }
387 
388 #[test]
test_save_buffer_to_file_001()389 fn test_save_buffer_to_file_001() {
390     let filename = FILE_PATH.to_string()  + ".026";
391     let content = "ttxx".to_string();
392     create_test_file(&filename, &content);
393     let buff: Vec<c_char> = Vec::new();
394     let ret = file_ex::ffi::RustSaveBufferToFile(&filename, &buff, false);
395     assert!(ret);
396 
397     let mut load_result = String::new();
398     let ret = file_ex::ffi::RustLoadStringFromFile(&filename, &mut load_result);
399     let _err = remove_test_file(&filename);
400     assert!(ret);
401     assert_eq!(load_result, content);
402 }
403 
404 #[test]
test_save_buffer_to_file_002()405 fn test_save_buffer_to_file_002() {
406     let filename = FILE_PATH.to_string()  + ".027";
407     let content = "ttxx".to_string();
408     create_test_file(&filename, &content);
409 
410     let new_content: Vec<c_char> = vec!['x' as c_char, 'x' as c_char, 't' as c_char, 't' as c_char];
411     let ret = file_ex::ffi::RustSaveBufferToFile(&filename, &new_content, true);
412     assert!(ret);
413 
414     let mut load_result = String::new();
415     let ret = file_ex::ffi::RustLoadStringFromFile(&filename, &mut load_result);
416     let _err = remove_test_file(&filename);
417     assert!(ret);
418     assert_eq!(load_result, String::from_utf8_lossy(&new_content));
419 }
420 
421 #[test]
test_save_buffer_to_file_003()422 fn test_save_buffer_to_file_003() {
423     let filename = FILE_PATH.to_string() + ".028";
424     let content = "ttxx".to_string();
425     create_test_file(&filename, &content);
426 
427     let new_content: Vec<c_char> = vec!['x' as c_char, 'x' as c_char, 't' as c_char, 't' as c_char];
428     let mut ret = file_ex::ffi::RustSaveBufferToFile(&filename, &new_content, false);
429     assert!(ret);
430 
431     let mut load_result = String::new();
432     ret = file_ex::ffi::RustLoadStringFromFile(&filename, &mut load_result);
433     let _err = remove_test_file(&filename);
434     assert!(ret);
435     assert_eq!(&load_result, &(content + &String::from_utf8_lossy(&new_content)));
436 }
437 
438 #[test]
test_string_exists_in_file_001()439 fn test_string_exists_in_file_001() {
440     let str_value = "abc".to_string();
441     let filename = String::new();
442     assert!(!file_ex::ffi::RustStringExistsInFile(&filename, &str_value, true));
443     assert!(!str_value.is_empty());
444 }
445 
446 #[test]
test_string_exists_in_file_002()447 fn test_string_exists_in_file_002() {
448     let str_value = NULL_STR.to_string();
449     let filename = FILE_PATH.to_string() + ".030";
450     let content = "hello world!".to_string();
451     create_test_file(&filename, &content);
452     assert!(!file_ex::ffi::RustStringExistsInFile(&filename, &str_value, true));
453     let _err = remove_test_file(&filename);
454 }
455 
456 #[test]
test_string_exists_in_file_003()457 fn test_string_exists_in_file_003() {
458     let str_value = "world".to_string();
459     let filename = FILE_PATH.to_string() + ".031";
460     let content = "hello world!".to_string();
461     create_test_file(&filename, &content);
462     assert!(file_ex::ffi::RustStringExistsInFile(&filename, &str_value, true));
463     let _err = remove_test_file(&filename);
464 }
465 
466 #[test]
test_string_exists_in_file_004()467 fn test_string_exists_in_file_004() {
468     let str_value1 = "t".repeat(MAX_FILE_LENGTH + 1);
469     let str_value2 = "t".repeat(MAX_FILE_LENGTH);
470     let filename = FILE_PATH.to_string() + ".032";
471     let content = "t".repeat(MAX_FILE_LENGTH);
472     create_test_file(&filename, &content);
473     assert!(!file_ex::ffi::RustStringExistsInFile(&filename, &str_value1, true));
474     assert!(file_ex::ffi::RustStringExistsInFile(&filename, &str_value2, true));
475     let _err = remove_test_file(&filename);
476 }
477 
478 #[test]
test_string_exists_in_file_005()479 fn test_string_exists_in_file_005() {
480     let str_value = "woRld".to_string();
481     let filename = FILE_PATH.to_string() + ".033";
482     let content = "hello world!".to_string();
483     create_test_file(&filename, &content);
484     assert!(file_ex::ffi::RustStringExistsInFile(&filename, &str_value, false));
485     assert!(!file_ex::ffi::RustStringExistsInFile(&filename, &str_value, true));
486     let _err = remove_test_file(&filename);
487 }
488 
489 #[test]
test_string_exists_in_file_006()490 fn test_string_exists_in_file_006() {
491     let str_value1 = "woRld".to_string();
492     let str_value2 = "123".to_string();
493     let str_value3 = "llo ".to_string();
494     let str_value4 = "123 w".to_string();
495     let str_value5 = "hi".to_string();
496     let filename = FILE_PATH.to_string() + ".034";
497     let content = "Test, hello 123 World!".to_string();
498     create_test_file(&filename, &content);
499     assert!(file_ex::ffi::RustStringExistsInFile(&filename, &str_value1, false));
500     assert!(!file_ex::ffi::RustStringExistsInFile(&filename, &str_value1, true));
501 
502     assert!(file_ex::ffi::RustStringExistsInFile(&filename, &str_value2, false));
503     assert!(file_ex::ffi::RustStringExistsInFile(&filename, &str_value2, true));
504 
505     assert!(file_ex::ffi::RustStringExistsInFile(&filename, &str_value3, false));
506     assert!(file_ex::ffi::RustStringExistsInFile(&filename, &str_value3, true));
507 
508     assert!(file_ex::ffi::RustStringExistsInFile(&filename, &str_value4, false));
509     assert!(!file_ex::ffi::RustStringExistsInFile(&filename, &str_value4, true));
510 
511     assert!(!file_ex::ffi::RustStringExistsInFile(&filename, &str_value5, false));
512     assert!(!file_ex::ffi::RustStringExistsInFile(&filename, &str_value5, true));
513     let _err = remove_test_file(&filename);
514 }
515 
516 #[test]
test_string_exists_in_file_007()517 fn test_string_exists_in_file_007() {
518     let str_value1 = "is".to_string();
519     let str_value2 = "\n\ris".to_string();
520     let filename = FILE_PATH.to_string() + ".035";
521     let content = "Test, special string\n\ris ok".to_string();
522     create_test_file(&filename, &content);
523     assert!(file_ex::ffi::RustStringExistsInFile(&filename, &str_value1, false));
524     assert!(file_ex::ffi::RustStringExistsInFile(&filename, &str_value1, true));
525 
526     assert!(file_ex::ffi::RustStringExistsInFile(&filename, &str_value2, false));
527     assert!(file_ex::ffi::RustStringExistsInFile(&filename, &str_value2, true));
528     let _err = remove_test_file(&filename);
529 }
530 
531 #[test]
test_file_exist_001()532 fn test_file_exist_001()
533 {
534     let filepath = "/proc/meminfo".to_string();
535     let filepath1 = "/proc/meminfo1".to_string();
536 
537     assert!(file_ex::ffi::RustFileExists(&filepath));
538     assert!(!(file_ex::ffi::RustFileExists(&filepath1)));
539 }
540 
541 #[test]
test_count_str_in_file_001()542 fn test_count_str_in_file_001()
543 {
544     let str = "abc".to_string();
545     let filename = "".to_string();
546     assert_eq!(file_ex::ffi::RustCountStrInFile(&filename, &str, true), -1);
547     assert!(!str.is_empty());
548 }
549 
550 #[test]
test_count_str_in_file_002()551 fn test_count_str_in_file_002()
552 {
553     let str = NULL_STR.to_string();
554     let filename = FILE_PATH.to_string() + ".038";
555     let content = "hello world!".to_string();
556     create_test_file(&filename, &content);
557     assert_eq!(file_ex::ffi::RustCountStrInFile(&filename, &str, true), -1);
558     let _err = remove_test_file(&filename);
559 }
560 
561 #[test]
test_count_str_in_file_003()562 fn test_count_str_in_file_003()
563 {
564     let str1 = "t".repeat(MAX_FILE_LENGTH + 1);
565     let str2 = "t".repeat(MAX_FILE_LENGTH);
566     let filename = FILE_PATH.to_string() + ".039";
567     let content = "t".repeat(MAX_FILE_LENGTH);
568     create_test_file(&filename, &content);
569     assert_eq!(file_ex::ffi::RustCountStrInFile(&filename, &str1, true), 0);
570     assert_eq!(file_ex::ffi::RustCountStrInFile(&filename, &str2, true), 1);
571     let _err = remove_test_file(&filename);
572 }
573 
574 #[test]
test_count_str_in_file_004()575 fn test_count_str_in_file_004()
576 {
577     let str1 = "very".to_string();
578     let str2 = "VERY".to_string();
579     let str3 = "abc".to_string();
580     let filename = FILE_PATH.to_string() + ".040";
581     let content = "This is very very long string for test.\n Very Good,\r VERY HAPPY.".to_string();
582     create_test_file(&filename, &content);
583     assert_eq!(file_ex::ffi::RustCountStrInFile(&filename, &str1, true), 2);
584     assert_eq!(file_ex::ffi::RustCountStrInFile(&filename, &str1, false), 4);
585 
586     assert_eq!(file_ex::ffi::RustCountStrInFile(&filename, &str2, true), 1);
587     assert_eq!(file_ex::ffi::RustCountStrInFile(&filename, &str2, false), 4);
588 
589     assert_eq!(file_ex::ffi::RustCountStrInFile(&filename, &str3, true), 0);
590     let _err = remove_test_file(&filename);
591 }
592 
593 #[test]
test_count_str_in_file_005()594 fn test_count_str_in_file_005()
595 {
596     let str1 = "aba".to_string();
597     let filename = FILE_PATH.to_string() + ".041";
598     let content = "This is abababaBABa.".to_string();
599     create_test_file(&filename, &content);
600     assert_eq!(file_ex::ffi::RustCountStrInFile(&filename, &str1, true), 2);
601     assert_eq!(file_ex::ffi::RustCountStrInFile(&filename, &str1, false), 3);
602     let _err = remove_test_file(&filename);
603 }