1 | // |
---|

2 | // Lol Engine |
---|

3 | // |
---|

4 | // Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net> |
---|

5 | // This program is free software; you can redistribute it and/or |
---|

6 | // modify it under the terms of the Do What The Fuck You Want To |
---|

7 | // Public License, Version 2, as published by Sam Hocevar. See |
---|

8 | // http://www.wtfpl.net/ for more details. |
---|

9 | // |
---|

10 | |
---|

11 | #if defined HAVE_CONFIG_H |
---|

12 | # include "config.h" |
---|

13 | #endif |
---|

14 | |
---|

15 | #include "core.h" |
---|

16 | #include "lol/unit.h" |
---|

17 | |
---|

18 | namespace lol |
---|

19 | { |
---|

20 | |
---|

21 | LOLUNIT_FIXTURE(ComplexTest) |
---|

22 | { |
---|

23 | void SetUp() {} |
---|

24 | |
---|

25 | void TearDown() {} |
---|

26 | |
---|

27 | LOLUNIT_TEST(Equality) |
---|

28 | { |
---|

29 | cmplx a2(1.0f, 2.0f); |
---|

30 | cmplx b2(0.0f, 2.0f); |
---|

31 | cmplx c2(1.0f, 0.0f); |
---|

32 | |
---|

33 | LOLUNIT_ASSERT_EQUAL(a2, a2); |
---|

34 | LOLUNIT_ASSERT_NOT_DIFFERENT(a2, a2); |
---|

35 | |
---|

36 | LOLUNIT_ASSERT_DIFFERENT(a2, b2); |
---|

37 | LOLUNIT_ASSERT_NOT_EQUAL(a2, b2); |
---|

38 | LOLUNIT_ASSERT_DIFFERENT(a2, c2); |
---|

39 | LOLUNIT_ASSERT_NOT_EQUAL(a2, c2); |
---|

40 | } |
---|

41 | |
---|

42 | LOLUNIT_TEST(UnaryMinus) |
---|

43 | { |
---|

44 | cmplx a(1.0f, 3.0f); |
---|

45 | cmplx b(-1.0f, -3.0f); |
---|

46 | |
---|

47 | LOLUNIT_ASSERT_EQUAL(a, -b); |
---|

48 | LOLUNIT_ASSERT_EQUAL(-a, b); |
---|

49 | } |
---|

50 | |
---|

51 | LOLUNIT_TEST(Conjugate) |
---|

52 | { |
---|

53 | cmplx a(1.0f, 3.0f); |
---|

54 | cmplx b(1.0f, -3.0f); |
---|

55 | |
---|

56 | LOLUNIT_ASSERT_EQUAL(a, ~b); |
---|

57 | LOLUNIT_ASSERT_EQUAL(~a, b); |
---|

58 | } |
---|

59 | |
---|

60 | LOLUNIT_TEST(Norm) |
---|

61 | { |
---|

62 | cmplx a(3.0f, -4.0f); |
---|

63 | |
---|

64 | LOLUNIT_ASSERT_EQUAL(a.norm(), 5.0f); |
---|

65 | |
---|

66 | cmplx b = a * ~a; |
---|

67 | cmplx c = a.norm() * a.norm(); |
---|

68 | |
---|

69 | LOLUNIT_ASSERT_EQUAL(b, c); |
---|

70 | |
---|

71 | cmplx d(5.0f, 12.0f); |
---|

72 | |
---|

73 | LOLUNIT_ASSERT_EQUAL(d.norm(), 13.0f); |
---|

74 | LOLUNIT_ASSERT_EQUAL((a * d).norm(), a.norm() * d.norm()); |
---|

75 | } |
---|

76 | |
---|

77 | LOLUNIT_TEST(Base) |
---|

78 | { |
---|

79 | cmplx one(1.0f, 0.0f); |
---|

80 | cmplx i(0.0f, 1.0f); |
---|

81 | |
---|

82 | LOLUNIT_ASSERT_EQUAL(one.norm(), 1.0f); |
---|

83 | LOLUNIT_ASSERT_EQUAL(i.norm(), 1.0f); |
---|

84 | |
---|

85 | LOLUNIT_ASSERT_EQUAL(i * i, -one); |
---|

86 | } |
---|

87 | |
---|

88 | LOLUNIT_TEST(Normalize) |
---|

89 | { |
---|

90 | cmplx a(3.0f, -4.0f); |
---|

91 | cmplx b = normalize(a); |
---|

92 | |
---|

93 | LOLUNIT_ASSERT_DOUBLES_EQUAL(b.norm(), 1.0, 1e-8); |
---|

94 | } |
---|

95 | |
---|

96 | LOLUNIT_TEST(Reciprocal) |
---|

97 | { |
---|

98 | cmplx a(3.0f, -4.0f); |
---|

99 | cmplx b = re(a); |
---|

100 | |
---|

101 | LOLUNIT_ASSERT_EQUAL(a * b, b * a); |
---|

102 | |
---|

103 | cmplx c = 1.0f; |
---|

104 | |
---|

105 | LOLUNIT_ASSERT_EQUAL(a * b, c); |
---|

106 | } |
---|

107 | }; |
---|

108 | |
---|

109 | } /* namespace lol */ |
---|

110 | |
---|