2
0

test_layer_cache.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* Copyright 2018 Fred Sundvik
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "test_common.hpp"
  17. #if MAX_LAYER_BITS != 5
  18. #error "Tese tests assume that the MAX_LAYER_BITS is equal to 5"
  19. // If this is changed, change the constants below
  20. #endif
  21. #if MATRIX_COLS != 2 || MATRIX_ROWS !=2
  22. #error "These tests assume that the second row starts after the second column"
  23. #endif
  24. namespace
  25. {
  26. constexpr uint8_t max_layer_value = 0b11111;
  27. constexpr uint8_t min_layer_value = 0;
  28. constexpr uint8_t alternating_starting_with_1 = 0b10101;
  29. constexpr uint8_t alternating_starting_with_0 = 0b01010;
  30. uint8_t read_cache(uint8_t col, uint8_t row) {
  31. keypos_t key;
  32. key.col = col;
  33. key.row = row;
  34. return read_source_layers_cache(key);
  35. }
  36. void write_cache(uint8_t col, uint8_t row, uint8_t value) {
  37. keypos_t key;
  38. key.col = col;
  39. key.row = row;
  40. return update_source_layers_cache(key, value);
  41. }
  42. void fill_cache() {
  43. for (int i=0; i < MATRIX_ROWS; i++) {
  44. for (int j=0; j < MATRIX_COLS; j++) {
  45. write_cache(j, i, max_layer_value);
  46. }
  47. }
  48. }
  49. void clear_cache() {
  50. for (int i=0; i < MATRIX_ROWS; i++) {
  51. for (int j=0; j < MATRIX_COLS; j++) {
  52. write_cache(j, i, min_layer_value);
  53. }
  54. }
  55. }
  56. }
  57. class LayerCache : public testing::Test
  58. {
  59. public:
  60. LayerCache()
  61. {
  62. clear_cache();
  63. }
  64. };
  65. TEST_F(LayerCache, LayerCacheIsInitializedToZero) {
  66. for (int i=0; i < MATRIX_ROWS; i++) {
  67. for (int j=0; j < MATRIX_COLS; j++) {
  68. EXPECT_EQ(read_cache(j, i), min_layer_value);
  69. }
  70. }
  71. }
  72. TEST_F(LayerCache, FillAndClearCache) {
  73. fill_cache();
  74. clear_cache();
  75. for (int i=0; i < MATRIX_ROWS; i++) {
  76. for (int j=0; j < MATRIX_COLS; j++) {
  77. EXPECT_EQ(read_cache(j, i), min_layer_value);
  78. }
  79. }
  80. }
  81. TEST_F(LayerCache, WriteAndReadFirstPosMaximumValue) {
  82. write_cache(0, 0, max_layer_value);
  83. EXPECT_EQ(read_cache(0, 0), max_layer_value);
  84. // The second position should not be updated
  85. EXPECT_EQ(read_cache(1, 0), min_layer_value);
  86. EXPECT_EQ(read_cache(0, 1), min_layer_value);
  87. }
  88. TEST_F(LayerCache, WriteAndReadSecondPosMaximumValue) {
  89. write_cache(1, 0, max_layer_value);
  90. EXPECT_EQ(read_cache(1, 0), max_layer_value);
  91. // The surrounding positions should not be updated
  92. EXPECT_EQ(read_cache(0, 0), min_layer_value);
  93. EXPECT_EQ(read_cache(0, 1), min_layer_value);
  94. }
  95. TEST_F(LayerCache, WriteAndReadFirstPosAlternatingBitsStartingWith1) {
  96. write_cache(0, 0, alternating_starting_with_1);
  97. EXPECT_EQ(read_cache(0, 0), alternating_starting_with_1);
  98. // The second position should not be updated
  99. EXPECT_EQ(read_cache(1, 0), min_layer_value);
  100. EXPECT_EQ(read_cache(0, 1), min_layer_value);
  101. }
  102. TEST_F(LayerCache, WriteAndReadSecondPosAlternatingBitsStartingWith1) {
  103. write_cache(1, 0, alternating_starting_with_1);
  104. EXPECT_EQ(read_cache(1, 0), alternating_starting_with_1);
  105. // The surrounding positions should not be updated
  106. EXPECT_EQ(read_cache(0, 0), min_layer_value);
  107. EXPECT_EQ(read_cache(0, 1), min_layer_value);
  108. }
  109. TEST_F(LayerCache, WriteAndReadFirstPosAlternatingBitsStartingWith0) {
  110. write_cache(0, 0, alternating_starting_with_0);
  111. EXPECT_EQ(read_cache(0, 0), alternating_starting_with_0);
  112. // The second position should not be updated
  113. EXPECT_EQ(read_cache(1, 0), min_layer_value);
  114. EXPECT_EQ(read_cache(0, 1), min_layer_value);
  115. }
  116. TEST_F(LayerCache, WriteAndReadSecondPosAlternatingBitsStartingWith0) {
  117. write_cache(1, 0, alternating_starting_with_0);
  118. EXPECT_EQ(read_cache(1, 0), alternating_starting_with_0);
  119. // The surrounding positions should not be updated
  120. EXPECT_EQ(read_cache(0, 0), min_layer_value);
  121. EXPECT_EQ(read_cache(0, 1), min_layer_value);
  122. }