Add helper method to create sized files, use meaningful names and add kibi and kilo tests.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Jonathan · committed by David Peter · parent 409cf0c · Browse files · 1 file·+45 added−16 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+45 added−16 removed
- tests:+45 added−16 removed
Provenance
- Commit
e2b2701- Tree
05afbd1- Author
Jonathan <joniweb01@gmail.com>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
409cf0c
1 file changed+45 added−16 removed
modified:tests/tests.rs+45 added-16 removed
13 unmodified lines14 15 use regex::escape;16 use std::fs;17 use std::io::Write;18+use std::path::Path;19 use testenv::TestEnv;20 21 static DEFAULT_DIRS: &'static [&'static str] = &["one/two/three", "one/two/three/directory_foo"];22 29 unmodified lines52 let root_path = get_absolute_root_path(&env);53 (env, root_path)54 }55 56+#[cfg(test)]57+fn create_file_with_size<P: AsRef<Path>>(path: P, size_in_bytes: usize) {58+ let content = "#".repeat(size_in_bytes);59+ let mut f = fs::File::create::<P>(path).unwrap();60+ f.write(content.as_bytes()).unwrap();61+}62+63 /// Simple tests64 #[test]65 fn test_simple() {66 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);949 unmodified lines1016 let dirs = &["test1", "test2"];1017 let files = &["test1/a.foo", "test1/b.foo", "test1/c.foo"];1018 let te = TestEnv::new(dirs, files);1019 1012- let mut f = fs::File::create(te.test_root().join("test1/tiny.foo")).unwrap();1013- f.write("Hello world".as_bytes()).unwrap(); // 11 bytes1014- f.flush().unwrap();1020+ create_file_with_size(te.test_root().join("test1/11_bytes.foo"), 11);1021+ create_file_with_size(te.test_root().join("test2/30_bytes.foo"), 30);1022+ create_file_with_size(te.test_root().join("test1/3_kilobytes.foo"), 3 * 1000);1023+ create_file_with_size(te.test_root().join("test2/4_kibibytes.foo"), 4 * 1024);1024 1016- let mut f = fs::File::create(te.test_root().join("test2/big.foo")).unwrap();1017- f.write("Hello user, fd is a nice tool.".as_bytes()).unwrap(); // 30 bytes1018- f.flush().unwrap();1019-1025 // Zero and non-zero sized files.1026 te.assert_output(1027 &["", "test1", "test2", "--size", "+0B"],1023- "test1/a.foo1028+ "test1/11_bytes.foo1029+ test1/a.foo1030 test1/b.foo1031 test1/c.foo1026- test1/tiny.foo1027- test2/big.foo"1032+ test1/3_kilobytes.foo1033+ test2/30_bytes.foo1034+ test2/4_kibibytes.foo"1035 );1036 1037 // Zero sized files.1038 te.assert_output(5 unmodified lines1044 1045 // Files with 2 bytes or more.1046 te.assert_output(1047 &["", "test1", "test2", "--size", "+2B"],1041- "test1/tiny.foo1042- test2/big.foo"1048+ "test1/11_bytes.foo1049+ test1/3_kilobytes.foo1050+ test2/30_bytes.foo1051+ test2/4_kibibytes.foo"1052 );1053 1054 // Files with 2 bytes or less.1055 te.assert_output(5 unmodified lines1061 1062 // Files with size between 1 byte and 11 bytes.1063 te.assert_output(1064 &["", "test1", "test2", "--size", "+1B", "--size", "-11B"],1056- "test1/tiny.foo"1065+ "test1/11_bytes.foo"1066 );1067 1068 // Files with size between 1 byte and 30 bytes.1069 te.assert_output(1070 &["", "test1", "test2", "--size", "+1B", "--size", "-30B"],1062- "test1/tiny.foo1063- test2/big.foo"1071+ "test1/11_bytes.foo1072+ test2/30_bytes.foo"1073 );1074 1075 // Files with size between 12 and 30 bytes.1076 te.assert_output(1077 &["", "test1", "test2", "--size", "+12B", "--size", "-30B"],1069- "test2/big.foo"1078+ "test2/30_bytes.foo"1079 );1080 1081 // Files with size between 31 and 100 bytes.1082 te.assert_output(1083 &["", "test1", "test2", "--size", "+31B", "--size", "-100B"],1084 ""1085 );1086+1087+ // Files with size between 3 kibibytes and 5 kibibytes.1088+ te.assert_output(1089+ &["", "test1", "test2", "--size", "+3ki", "--size", "-5ki"],1090+ "test2/4_kibibytes.foo"1091+ );1092+1093+ // Files with size between 3 kilobytes and 5 kilobytes.1094+ te.assert_output(1095+ &["", "test1", "test2", "--size", "+3k", "--size", "-5k"],1096+ "test1/3_kilobytes.foo1097+ test2/4_kibibytes.foo"1098+ );1099+1100+ // Files with size greater than 3 kilobytes and less than 4 kibibytes.1101+ te.assert_output(1102+ &["", "test1", "test2", "--size", "+3k", "--size", "-3ki"],1103+ "test1/3_kilobytes.foo"1104+ );1105+1106 }