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.

cmplx.cpp 2.3 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //
  2. // Lol Engine — Unit tests
  3. //
  4. // Copyright © 2010—2020 Sam Hocevar <sam@hocevar.net>
  5. //
  6. // Lol Engine is free software. It comes without any warranty, to
  7. // the extent permitted by applicable law. You can redistribute it
  8. // and/or modify it under the terms of the Do What the Fuck You Want
  9. // to Public License, Version 2, as published by the WTFPL Task Force.
  10. // See http://www.wtfpl.net/ for more details.
  11. //
  12. #if HAVE_CONFIG_H
  13. # include "config.h"
  14. #endif
  15. #include <lol/unit_test>
  16. #include <lol/transform>
  17. namespace lol
  18. {
  19. lolunit_declare_fixture(complex_test)
  20. {
  21. lolunit_declare_test(equality)
  22. {
  23. cmplx a2(1.0f, 2.0f);
  24. cmplx b2(0.0f, 2.0f);
  25. cmplx c2(1.0f, 0.0f);
  26. lolunit_assert_equal(a2, a2);
  27. lolunit_refute_different(a2, a2);
  28. lolunit_assert_different(a2, b2);
  29. lolunit_refute_equal(a2, b2);
  30. lolunit_assert_different(a2, c2);
  31. lolunit_refute_equal(a2, c2);
  32. }
  33. lolunit_declare_test(unary_minus)
  34. {
  35. cmplx a(1.0f, 3.0f);
  36. cmplx b(-1.0f, -3.0f);
  37. lolunit_assert_equal(a, -b);
  38. lolunit_assert_equal(-a, b);
  39. }
  40. lolunit_declare_test(conjugate)
  41. {
  42. cmplx a(1.0f, 3.0f);
  43. cmplx b(1.0f, -3.0f);
  44. lolunit_assert_equal(a, ~b);
  45. lolunit_assert_equal(~a, b);
  46. }
  47. lolunit_declare_test(complex_norm)
  48. {
  49. cmplx a(3.0f, -4.0f);
  50. lolunit_assert_equal(norm(a), 5.0f);
  51. cmplx b = a * ~a;
  52. cmplx c = norm(a) * norm(a);
  53. lolunit_assert_equal(b, c);
  54. cmplx d(5.0f, 12.0f);
  55. lolunit_assert_equal(norm(d), 13.0f);
  56. lolunit_assert_equal(norm(a * d), norm(a) * norm(d));
  57. }
  58. lolunit_declare_test(base)
  59. {
  60. cmplx one(1.0f, 0.0f);
  61. cmplx i(0.0f, 1.0f);
  62. lolunit_assert_equal(norm(one), 1.0f);
  63. lolunit_assert_equal(norm(i), 1.0f);
  64. lolunit_assert_equal(i * i, -one);
  65. }
  66. lolunit_declare_test(complex_normalize)
  67. {
  68. cmplx a(3.0f, -4.0f);
  69. cmplx b = normalize(a);
  70. lolunit_assert_doubles_equal(norm(b), 1.0, 1e-8);
  71. }
  72. lolunit_declare_test(complex_inverse)
  73. {
  74. cmplx a(3.0f, -4.0f);
  75. cmplx b = inverse(a);
  76. lolunit_assert_equal(a * b, b * a);
  77. cmplx c = 1.0f;
  78. lolunit_assert_equal(a * b, c);
  79. }
  80. };
  81. } /* namespace lol */