You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
5.2 KiB

  1. // Copyright 2015 Drew Short <drew@sothr.com>.
  2. //
  3. // Licensed under the MIT license<LICENSE-MIT or http://opensource.org/licenses/MIT>.
  4. // This file may not be copied, modified, or distributed except according to those terms.
  5. mod hash;
  6. pub fn hello(mut result: String) -> String {
  7. let helloworld = "Hello, World!\n";
  8. result.push_str(helloworld);
  9. let n = 1u8;
  10. let ns = format!("1: {:b}\n", n);
  11. let n2 = 2u8;
  12. let n2s = format!("2: {:b}\n", n2);
  13. result.push_str(&ns);
  14. result.push_str(&n2s);
  15. let mut endian = "Big Endian\n";
  16. if cfg!(target_endian = "big") {
  17. result.push_str(endian);
  18. } else {
  19. endian = "Little Endian\n";
  20. result.push_str(endian);
  21. }
  22. result
  23. }
  24. /*
  25. * Module for the tests
  26. */
  27. #[cfg(test)]
  28. mod tests {
  29. use super::*;
  30. use std::fs;
  31. use std::path;
  32. use hash;
  33. #[test]
  34. fn can_get_test_images() {
  35. let paths = fs::read_dir(&path::Path::new("./test_images")).unwrap();
  36. let mut num_paths = 0;
  37. for path in paths {
  38. let orig_path = path.unwrap().path();
  39. let ext = path::Path::new(&orig_path).extension();
  40. match ext {
  41. Some(_) => {
  42. if ext.unwrap() == "jpg" {
  43. num_paths += 1;
  44. println!("Is a image {}: {:?}", num_paths, orig_path) ;
  45. }
  46. },
  47. _ => {
  48. println!("Not an image: {:?}", orig_path) ;
  49. continue
  50. }
  51. }
  52. //println!("Name: {}", path.unwrap().path().display())
  53. }
  54. // Currently 12 images in the test imaages directory
  55. assert!(num_paths == 12);
  56. }
  57. // Simple function for the unit tests to succinctly test a set of images
  58. // that are organized in the fashion of large->medium->small
  59. fn test_image_set_ahash(
  60. large_path: &str,
  61. medium_path: &str,
  62. small_path: &str,
  63. expected_large_hash: u64,
  64. expected_medium_hash: u64,
  65. expected_small_hash: u64,
  66. expected_large_medium_hamming: u64,
  67. expected_large_small_hamming: u64,
  68. expected_medium_small_hamming: u64) {
  69. let large_prepared_image = hash::prepare_image(path::Path::new(large_path), 8u32);
  70. let medium_prepared_image = hash::prepare_image(path::Path::new(medium_path), 8u32);
  71. let small_prepared_image = hash::prepare_image(path::Path::new(small_path), 8u32);
  72. let large_ahash = hash::get_ahash(large_prepared_image);
  73. let medium_ahash = hash::get_ahash(medium_prepared_image);
  74. let small_ahash = hash::get_ahash(small_prepared_image);
  75. println!("{}: {}", large_path, large_ahash);
  76. println!("{}: {}", medium_path, medium_ahash);
  77. println!("{}: {}", small_path, small_ahash);
  78. assert!(large_ahash == expected_large_hash);
  79. assert!(medium_ahash == expected_medium_hash);
  80. assert!(small_ahash == expected_small_hash);
  81. let large_medium_hamming = hash::calculate_hamming_distance(large_ahash, medium_ahash);
  82. let large_small_hamming = hash::calculate_hamming_distance(large_ahash, small_ahash);
  83. let medium_small_hamming = hash::calculate_hamming_distance(medium_ahash, small_ahash);
  84. println!("Large-Medium Hamming Distance: {}", large_medium_hamming);
  85. println!("Large-Small Hamming Distance: {}", large_small_hamming);
  86. println!("Medium-Small Hamming Distance: {}", medium_small_hamming);
  87. assert!(large_medium_hamming == expected_large_medium_hamming);
  88. assert!(large_small_hamming == expected_large_small_hamming);
  89. assert!(medium_small_hamming == expected_medium_small_hamming);
  90. }
  91. #[test]
  92. fn confirm_ahash_results() {
  93. // Sample_01 tests
  94. test_image_set_ahash(
  95. "./test_images/sample_01_large.jpg",
  96. "./test_images/sample_01_medium.jpg",
  97. "./test_images/sample_01_small.jpg",
  98. 857051991849750,
  99. 857051991849750,
  100. 857051991849750,
  101. 0u64,
  102. 0u64,
  103. 0u64
  104. );
  105. // Sample_02 tests
  106. test_image_set_ahash(
  107. "./test_images/sample_02_large.jpg",
  108. "./test_images/sample_02_medium.jpg",
  109. "./test_images/sample_02_small.jpg",
  110. 18446744073441116160,
  111. 18446744073441116160,
  112. 18446744073441116160,
  113. 0u64,
  114. 0u64,
  115. 0u64
  116. );
  117. // Sample_03 tests
  118. test_image_set_ahash(
  119. "./test_images/sample_03_large.jpg",
  120. "./test_images/sample_03_medium.jpg",
  121. "./test_images/sample_03_small.jpg",
  122. 135670932300497406,
  123. 135670932300497406,
  124. 135670932300497406,
  125. 0u64,
  126. 0u64,
  127. 0u64
  128. );
  129. // Sample_04 tests
  130. test_image_set_ahash(
  131. "./test_images/sample_04_large.jpg",
  132. "./test_images/sample_04_medium.jpg",
  133. "./test_images/sample_04_small.jpg",
  134. 18446460933225054208,
  135. 18446460933090836480,
  136. 18446460933090836480,
  137. 1u64,
  138. 1u64,
  139. 0u64
  140. );
  141. }
  142. }