test_layer_cache.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. keymatrix_t key;
  32. key.pos.col = col;
  33. key.pos.row = row;
  34. key.matrix = 0;
  35. return read_source_layers_cache(key);
  36. }
  37. void write_cache(uint8_t col, uint8_t row, uint8_t value) {
  38. keymatrix_t key;
  39. key.pos.col = col;
  40. key.pos.row = row;
  41. key.matrix = 0;
  42. return update_source_layers_cache(key, value);
  43. }
  44. void fill_cache() {
  45. for (int i=0; i < MATRIX_ROWS; i++) {
  46. for (int j=0; j < MATRIX_COLS; j++) {
  47. write_cache(j, i, max_layer_value);
  48. }
  49. }
  50. }
  51. void clear_cache() {
  52. for (int i=0; i < MATRIX_ROWS; i++) {
  53. for (int j=0; j < MATRIX_COLS; j++) {
  54. write_cache(j, i, min_layer_value);
  55. }
  56. }
  57. }
  58. }
  59. class LayerCache : public testing::Test
  60. {
  61. public:
  62. LayerCache()
  63. {
  64. clear_cache();
  65. }
  66. };
  67. TEST_F(LayerCache, LayerCacheIsInitializedToZero) {
  68. for (int i=0; i < MATRIX_ROWS; i++) {
  69. for (int j=0; j < MATRIX_COLS; j++) {
  70. EXPECT_EQ(read_cache(j, i), min_layer_value);
  71. }
  72. }
  73. }
  74. TEST_F(LayerCache, FillAndClearCache) {
  75. fill_cache();
  76. clear_cache();
  77. for (int i=0; i < MATRIX_ROWS; i++) {
  78. for (int j=0; j < MATRIX_COLS; j++) {
  79. EXPECT_EQ(read_cache(j, i), min_layer_value);
  80. }
  81. }
  82. }
  83. TEST_F(LayerCache, WriteAndReadFirstPosMaximumValue) {
  84. write_cache(0, 0, max_layer_value);
  85. EXPECT_EQ(read_cache(0, 0), max_layer_value);
  86. // The second position should not be updated
  87. EXPECT_EQ(read_cache(1, 0), min_layer_value);
  88. EXPECT_EQ(read_cache(0, 1), min_layer_value);
  89. }
  90. TEST_F(LayerCache, WriteAndReadSecondPosMaximumValue) {
  91. write_cache(1, 0, max_layer_value);
  92. EXPECT_EQ(read_cache(1, 0), max_layer_value);
  93. // The surrounding positions should not be updated
  94. EXPECT_EQ(read_cache(0, 0), min_layer_value);
  95. EXPECT_EQ(read_cache(0, 1), min_layer_value);
  96. }
  97. TEST_F(LayerCache, WriteAndReadFirstPosAlternatingBitsStartingWith1) {
  98. write_cache(0, 0, alternating_starting_with_1);
  99. EXPECT_EQ(read_cache(0, 0), alternating_starting_with_1);
  100. // The second position should not be updated
  101. EXPECT_EQ(read_cache(1, 0), min_layer_value);
  102. EXPECT_EQ(read_cache(0, 1), min_layer_value);
  103. }
  104. TEST_F(LayerCache, WriteAndReadSecondPosAlternatingBitsStartingWith1) {
  105. write_cache(1, 0, alternating_starting_with_1);
  106. EXPECT_EQ(read_cache(1, 0), alternating_starting_with_1);
  107. // The surrounding positions should not be updated
  108. EXPECT_EQ(read_cache(0, 0), min_layer_value);
  109. EXPECT_EQ(read_cache(0, 1), min_layer_value);
  110. }
  111. TEST_F(LayerCache, WriteAndReadFirstPosAlternatingBitsStartingWith0) {
  112. write_cache(0, 0, alternating_starting_with_0);
  113. EXPECT_EQ(read_cache(0, 0), alternating_starting_with_0);
  114. // The second position should not be updated
  115. EXPECT_EQ(read_cache(1, 0), min_layer_value);
  116. EXPECT_EQ(read_cache(0, 1), min_layer_value);
  117. }
  118. TEST_F(LayerCache, WriteAndReadSecondPosAlternatingBitsStartingWith0) {
  119. write_cache(1, 0, alternating_starting_with_0);
  120. EXPECT_EQ(read_cache(1, 0), alternating_starting_with_0);
  121. // The surrounding positions should not be updated
  122. EXPECT_EQ(read_cache(0, 0), min_layer_value);
  123. EXPECT_EQ(read_cache(0, 1), min_layer_value);
  124. }