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.
 
 
 

124 lines
3.5 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2014 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. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #include <lol/main.h>
  14. #include "lol/unit.h"
  15. #include <iostream>
  16. namespace lol
  17. {
  18. LOLUNIT_FIXTURE(ArrayNDTest)
  19. {
  20. void SetUp() {}
  21. void TearDown() {}
  22. LOLUNIT_TEST(Array2D)
  23. {
  24. arraynd<2, int> a;
  25. a.SetSize(vec_t<int, 2>(2, 2));
  26. /* Non-const accessors */
  27. a[0][0] = 1;
  28. a[0][1] = 2;
  29. a[1][0] = 3;
  30. a[1][1] = 4;
  31. LOLUNIT_ASSERT_EQUAL(a[0][0], 1);
  32. LOLUNIT_ASSERT_EQUAL(a[0][1], 2);
  33. LOLUNIT_ASSERT_EQUAL(a[1][0], 3);
  34. LOLUNIT_ASSERT_EQUAL(a[1][1], 4);
  35. /* Const accessors */
  36. arraynd<2, int> const &b = a;
  37. LOLUNIT_ASSERT_EQUAL(b[0][0], 1);
  38. LOLUNIT_ASSERT_EQUAL(b[0][1], 2);
  39. LOLUNIT_ASSERT_EQUAL(b[1][0], 3);
  40. LOLUNIT_ASSERT_EQUAL(b[1][1], 4);
  41. }
  42. LOLUNIT_TEST(ArrayNDCreate)
  43. {
  44. arraynd<10, int> a;
  45. arraynd<20, float> b;
  46. arraynd<30, uint8_t> c;
  47. arraynd<40, double> d;
  48. arraynd<3, int> e = { { {1, 2}, {3, 4} }, { {5, 6}, {7, 8} } };
  49. LOLUNIT_ASSERT_EQUAL(e.GetSize()[0], 2);
  50. LOLUNIT_ASSERT_EQUAL(e.GetSize()[1], 2);
  51. LOLUNIT_ASSERT_EQUAL(e.GetSize()[2], 2);
  52. LOLUNIT_ASSERT_EQUAL(e[0][0][0], 1);
  53. LOLUNIT_ASSERT_EQUAL(e[1][0][0], 2);
  54. LOLUNIT_ASSERT_EQUAL(e[0][1][0], 3);
  55. LOLUNIT_ASSERT_EQUAL(e[1][1][0], 4);
  56. LOLUNIT_ASSERT_EQUAL(e[0][0][1], 5);
  57. LOLUNIT_ASSERT_EQUAL(e[1][0][1], 6);
  58. LOLUNIT_ASSERT_EQUAL(e[0][1][1], 7);
  59. LOLUNIT_ASSERT_EQUAL(e[1][1][1], 8);
  60. arraynd<3, int> f = { { {1, 2, 3, 4}, {5, 6, 7} }, { {8, 9}, {10} } };
  61. LOLUNIT_ASSERT_EQUAL(f.GetSize()[0], 4);
  62. LOLUNIT_ASSERT_EQUAL(f.GetSize()[1], 2);
  63. LOLUNIT_ASSERT_EQUAL(f.GetSize()[2], 2);
  64. LOLUNIT_ASSERT_EQUAL(f[0][0][0], 1);
  65. LOLUNIT_ASSERT_EQUAL(f[1][0][0], 2);
  66. LOLUNIT_ASSERT_EQUAL(f[2][0][0], 3);
  67. LOLUNIT_ASSERT_EQUAL(f[3][0][0], 4);
  68. LOLUNIT_ASSERT_EQUAL(f[0][1][0], 5);
  69. LOLUNIT_ASSERT_EQUAL(f[1][1][0], 6);
  70. LOLUNIT_ASSERT_EQUAL(f[2][1][0], 7);
  71. LOLUNIT_ASSERT_EQUAL(f[3][1][0], 0);
  72. LOLUNIT_ASSERT_EQUAL(f[0][0][1], 8);
  73. LOLUNIT_ASSERT_EQUAL(f[1][0][1], 9);
  74. LOLUNIT_ASSERT_EQUAL(f[2][0][1], 0);
  75. LOLUNIT_ASSERT_EQUAL(f[3][0][1], 0);
  76. LOLUNIT_ASSERT_EQUAL(f[0][1][1], 10);
  77. LOLUNIT_ASSERT_EQUAL(f[1][1][1], 0);
  78. LOLUNIT_ASSERT_EQUAL(f[2][1][1], 0);
  79. LOLUNIT_ASSERT_EQUAL(f[3][1][1], 0);
  80. }
  81. LOLUNIT_TEST(ArrayNDInit)
  82. {
  83. int const NDIM = 8;
  84. vec_t<int, NDIM> size;
  85. for (int i = 0; i < NDIM; ++i)
  86. size[i] = 5;
  87. arraynd<NDIM, uint8_t> a(size);
  88. memset(a.Data(), 0, a.Bytes());
  89. LOLUNIT_ASSERT_EQUAL(a[2][3][2][0][1][4][0][1], 0x00);
  90. vec_t<int, NDIM> v = { 2, 3, 2, 0, 1, 4, 0, 1 };
  91. LOLUNIT_ASSERT_EQUAL(a[v], 0x00);
  92. a[2][3][2][0][1][4][0][1] = 0xcd;
  93. LOLUNIT_ASSERT_EQUAL(a[2][3][2][0][1][4][0][1], 0xcd);
  94. LOLUNIT_ASSERT_EQUAL(a[v], 0xcd);
  95. arraynd<NDIM, uint8_t> const &b = a;
  96. LOLUNIT_ASSERT_EQUAL(b[2][3][2][0][1][4][0][1], 0xcd);
  97. LOLUNIT_ASSERT_EQUAL(b[v], 0xcd);
  98. }
  99. };
  100. } /* namespace lol */