oxedyne/fe2o3/fe2o3_geom/tests/shape_test.rs
4.8 KiB, 1 run
created by r1870400018:9355, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | use oxedyne_fe2o3_geom::shape::{ShapeF32, ShapeF64, ShapeU32}; |
| 2 | |
| 3 | #[test] |
| 4 | fn test_shapef32_area() { |
| 5 | // Circle with radius 5. |
| 6 | let circle = ShapeF32::Circle(5.0); |
| 7 | assert!((circle.area() - 78.53982).abs() < 0.001); |
| 8 | |
| 9 | // Rectangle 4x6. |
| 10 | let rect = ShapeF32::Rectangle(4.0, 6.0); |
| 11 | assert_eq!(rect.area(), 24.0); |
| 12 | |
| 13 | // Square with side 3. |
| 14 | let square = ShapeF32::Square(3.0); |
| 15 | assert_eq!(square.area(), 9.0); |
| 16 | } |
| 17 | |
| 18 | #[test] |
| 19 | fn test_shapef32_perimeter() { |
| 20 | // Circle with radius 5. |
| 21 | let circle = ShapeF32::Circle(5.0); |
| 22 | assert!((circle.perimeter() - 31.41593).abs() < 0.001); |
| 23 | |
| 24 | // Rectangle 4x6. |
| 25 | let rect = ShapeF32::Rectangle(4.0, 6.0); |
| 26 | assert_eq!(rect.perimeter(), 20.0); |
| 27 | |
| 28 | // Square with side 3. |
| 29 | let square = ShapeF32::Square(3.0); |
| 30 | assert_eq!(square.perimeter(), 12.0); |
| 31 | } |
| 32 | |
| 33 | #[test] |
| 34 | fn test_shapef64_area() { |
| 35 | // Circle with radius 10. |
| 36 | let circle = ShapeF64::Circle(10.0); |
| 37 | assert!((circle.area() - 314.159265358979).abs() < 0.00001); |
| 38 | |
| 39 | // Rectangle 5x8. |
| 40 | let rect = ShapeF64::Rectangle(5.0, 8.0); |
| 41 | assert_eq!(rect.area(), 40.0); |
| 42 | |
| 43 | // Square with side 7. |
| 44 | let square = ShapeF64::Square(7.0); |
| 45 | assert_eq!(square.area(), 49.0); |
| 46 | } |
| 47 | |
| 48 | #[test] |
| 49 | fn test_shapef64_perimeter() { |
| 50 | // Circle with radius 10. |
| 51 | let circle = ShapeF64::Circle(10.0); |
| 52 | assert!((circle.perimeter() - 62.83185307179586).abs() < 0.00001); |
| 53 | |
| 54 | // Rectangle 5x8. |
| 55 | let rect = ShapeF64::Rectangle(5.0, 8.0); |
| 56 | assert_eq!(rect.perimeter(), 26.0); |
| 57 | |
| 58 | // Square with side 7. |
| 59 | let square = ShapeF64::Square(7.0); |
| 60 | assert_eq!(square.perimeter(), 28.0); |
| 61 | } |
| 62 | |
| 63 | #[test] |
| 64 | fn test_shapeu32_area() { |
| 65 | // Circle with radius 10. |
| 66 | let circle = ShapeU32::Circle(10); |
| 67 | // PI ≈ 355/113, so area ≈ (355 * 100) / 113 = 314. |
| 68 | assert_eq!(circle.area(), 314); |
| 69 | |
| 70 | // Rectangle 5x8. |
| 71 | let rect = ShapeU32::Rectangle(5, 8); |
| 72 | assert_eq!(rect.area(), 40); |
| 73 | |
| 74 | // Square with side 7. |
| 75 | let square = ShapeU32::Square(7); |
| 76 | assert_eq!(square.area(), 49); |
| 77 | } |
| 78 | |
| 79 | #[test] |
| 80 | fn test_shapeu32_perimeter() { |
| 81 | // Circle with radius 10. |
| 82 | let circle = ShapeU32::Circle(10); |
| 83 | // 2*PI ≈ 2*355/113, so perimeter ≈ (2 * 355 * 10) / 113 = 62. |
| 84 | assert_eq!(circle.perimeter(), 62); |
| 85 | |
| 86 | // Rectangle 5x8. |
| 87 | let rect = ShapeU32::Rectangle(5, 8); |
| 88 | assert_eq!(rect.perimeter(), 26); |
| 89 | |
| 90 | // Square with side 7. |
| 91 | let square = ShapeU32::Square(7); |
| 92 | assert_eq!(square.perimeter(), 28); |
| 93 | } |
| 94 | |
| 95 | #[test] |
| 96 | fn test_shapef32_scale_by_area() { |
| 97 | // Circle with radius 5, scale by 4. |
| 98 | let circle = ShapeF32::Circle(5.0); |
| 99 | let scaled = circle.scale_by_area(4.0); |
| 100 | assert!((scaled.area() - circle.area() * 4.0).abs() < 0.001); |
| 101 | |
| 102 | // Rectangle 3x4, scale by 2.25. |
| 103 | let rect = ShapeF32::Rectangle(3.0, 4.0); |
| 104 | let scaled = rect.scale_by_area(2.25); |
| 105 | assert!((scaled.area() - rect.area() * 2.25).abs() < 0.001); |
| 106 | // Check aspect ratio preserved. |
| 107 | if let ShapeF32::Rectangle(w, h) = scaled { |
| 108 | assert!((w / h - 3.0 / 4.0).abs() < 0.001); |
| 109 | } |
| 110 | |
| 111 | // Square with side 2, scale by 9. |
| 112 | let square = ShapeF32::Square(2.0); |
| 113 | let scaled = square.scale_by_area(9.0); |
| 114 | assert!((scaled.area() - square.area() * 9.0).abs() < 0.001); |
| 115 | } |
| 116 | |
| 117 | #[test] |
| 118 | fn test_shapef64_scale_by_area() { |
| 119 | // Circle with radius 10, scale by 0.25. |
| 120 | let circle = ShapeF64::Circle(10.0); |
| 121 | let scaled = circle.scale_by_area(0.25); |
| 122 | assert!((scaled.area() - circle.area() * 0.25).abs() < 0.00001); |
| 123 | |
| 124 | // Rectangle 5x8, scale by 1.44. |
| 125 | let rect = ShapeF64::Rectangle(5.0, 8.0); |
| 126 | let scaled = rect.scale_by_area(1.44); |
| 127 | assert!((scaled.area() - rect.area() * 1.44).abs() < 0.00001); |
| 128 | // Check aspect ratio preserved. |
| 129 | if let ShapeF64::Rectangle(w, h) = scaled { |
| 130 | assert!((w / h - 5.0 / 8.0).abs() < 0.00001); |
| 131 | } |
| 132 | |
| 133 | // Square with side 7, scale by 0.5. |
| 134 | let square = ShapeF64::Square(7.0); |
| 135 | let scaled = square.scale_by_area(0.5); |
| 136 | assert!((scaled.area() - square.area() * 0.5).abs() < 0.00001); |
| 137 | } |
| 138 | |
| 139 | #[test] |
| 140 | fn test_shapeu32_scale_by_area() { |
| 141 | // Circle with radius 10, scale by 4. |
| 142 | let circle = ShapeU32::Circle(10); |
| 143 | let scaled = circle.scale_by_area(4); |
| 144 | // Allow for integer rounding. |
| 145 | let expected_area = circle.area() * 4; |
| 146 | let actual_area = scaled.area(); |
| 147 | assert!((actual_area as i32 - expected_area as i32).abs() < 10); |
| 148 | |
| 149 | // Rectangle 6x8, scale by 4. |
| 150 | let rect = ShapeU32::Rectangle(6, 8); |
| 151 | let scaled = rect.scale_by_area(4); |
| 152 | assert_eq!(scaled.area(), rect.area() * 4); |
| 153 | // Check aspect ratio preserved. |
| 154 | if let ShapeU32::Rectangle(w, h) = scaled { |
| 155 | assert_eq!(w * 8, h * 6); // Cross multiply to check ratio. |
| 156 | } |
| 157 | |
| 158 | // Square with side 5, scale by 9. |
| 159 | let square = ShapeU32::Square(5); |
| 160 | let scaled = square.scale_by_area(9); |
| 161 | assert_eq!(scaled.area(), square.area() * 9); |
| 162 | } |