|
| 1 | +///////////////////////////////////////////////////////////// |
| 2 | +// Vector Math Tests |
| 3 | +///////////////////////////////////////////////////////////// |
| 4 | + |
| 5 | +// Swizzle |
| 6 | +void test_float2_swizzle() |
| 7 | +{ |
| 8 | + test.beginTest("float2_swizzle"); |
| 9 | + float2 a = float2(1, 2); |
| 10 | + // float2 -> float2 swizzles. |
| 11 | + test.expectEq(a.xy, float2(1,2)); |
| 12 | + test.expectEq(a.yx, float2(2,1)); |
| 13 | + test.expectEq(a.xx, float2(1,1)); |
| 14 | + test.expectEq(a.yy, float2(2,2)); |
| 15 | + // float2 -> scalar swizzles. |
| 16 | + test.expectEq(a.x, 1.0f); |
| 17 | + test.expectEq(a.y, 2.0f); |
| 18 | +} |
| 19 | + |
| 20 | +// float2 x float2 math |
| 21 | +void test_float2_float2_math() |
| 22 | +{ |
| 23 | + test.beginTest("float2_float2_math"); |
| 24 | + float2 a = float2(1, 2); |
| 25 | + float2 b = float2(3, 4); |
| 26 | + float2 c = float2(0, 1); |
| 27 | + test.expectEq(a + b, float2(4, 6)); |
| 28 | + test.expectEq(a - b, float2(-2, -2)); |
| 29 | + test.expectEq(a * b, float2(3, 8)); |
| 30 | + test.expectEq(a / b, float2(1.0/3.0, 2.0/4.0)); |
| 31 | + |
| 32 | + const float neg45 = math.radians(-45.0); // -45 degrees in radians. |
| 33 | + // Only concern ourselves with math functions that effect vector types, |
| 34 | + // scalar/per-component functions should be tested separately. |
| 35 | + test.expectEq(math.distance(a, b), 2.828427125f); |
| 36 | + test.expectEq(math.dot(a, b), 11.0f); |
| 37 | + test.expectEq(math.length(a), 2.2360679775f); |
| 38 | + test.expectEq(math.normalize(a), float2(0.4472135955, 0.894427191)); |
| 39 | + test.expectEq(math.perp(a, b), -2.0f); |
| 40 | + test.expectEq(math.sincos(neg45), float2(-0.7071067812, 0.7071067812)); |
| 41 | + |
| 42 | + test.expectTrue(math.all(a)); |
| 43 | + test.expectTrue(math.any(a)); |
| 44 | + test.expectFalse(math.all(c)); |
| 45 | + test.expectTrue(math.any(c)); |
| 46 | +} |
| 47 | + |
| 48 | +// float2 x scalar math |
| 49 | +void test_float2_scalar_math() |
| 50 | +{ |
| 51 | + test.beginTest("float2_scalar_math"); |
| 52 | + float2 a = float2(3, 4); |
| 53 | + test.expectEq(a * 0.0, float2(0)); |
| 54 | + test.expectEq(a * 1.0, float2(3, 4)); |
| 55 | + test.expectEq(a.yx * 0.5, float2(2.0, 1.5)); |
| 56 | + |
| 57 | + test.expectEq(a / 2.0, float2(1.5, 2)); |
| 58 | + test.expectEq(a + 2.0, float2(5, 6)); |
| 59 | + test.expectEq(a - 2.0, float2(1, 2)); |
| 60 | + test.expectEq(a * 2.0, float2(6, 8)); |
| 61 | +} |
| 62 | + |
| 63 | +void main() |
| 64 | +{ |
| 65 | + test.beginSystem("Vector Math"); |
| 66 | + { |
| 67 | + test_float2_swizzle(); |
| 68 | + test_float2_float2_math(); |
| 69 | + test_float2_scalar_math(); |
| 70 | + } |
| 71 | + test.report(); |
| 72 | +} |
0 commit comments