| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- /* Copyright 2018 Fred Sundvik
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- #include "test_common.hpp"
- #if MAX_LAYER_BITS != 5
- #error "Tese tests assume that the MAX_LAYER_BITS is equal to 5"
- // If this is changed, change the constants below
- #endif
- #if MATRIX_COLS != 2 || MATRIX_ROWS !=2
- #error "These tests assume that the second row starts after the second column"
- #endif
- namespace
- {
- constexpr uint8_t max_layer_value = 0b11111;
- constexpr uint8_t min_layer_value = 0;
- constexpr uint8_t alternating_starting_with_1 = 0b10101;
- constexpr uint8_t alternating_starting_with_0 = 0b01010;
- uint8_t read_cache(uint8_t col, uint8_t row) {
- keymatrix_t key;
- key.pos.col = col;
- key.pos.row = row;
- key.matrix = 0;
- return read_source_layers_cache(key);
- }
- void write_cache(uint8_t col, uint8_t row, uint8_t value) {
- keymatrix_t key;
- key.pos.col = col;
- key.pos.row = row;
- key.matrix = 0;
- return update_source_layers_cache(key, value);
- }
- void fill_cache() {
- for (int i=0; i < MATRIX_ROWS; i++) {
- for (int j=0; j < MATRIX_COLS; j++) {
- write_cache(j, i, max_layer_value);
- }
- }
- }
- void clear_cache() {
- for (int i=0; i < MATRIX_ROWS; i++) {
- for (int j=0; j < MATRIX_COLS; j++) {
- write_cache(j, i, min_layer_value);
- }
- }
- }
- }
- class LayerCache : public testing::Test
- {
- public:
- LayerCache()
- {
- clear_cache();
- }
- };
- TEST_F(LayerCache, LayerCacheIsInitializedToZero) {
- for (int i=0; i < MATRIX_ROWS; i++) {
- for (int j=0; j < MATRIX_COLS; j++) {
- EXPECT_EQ(read_cache(j, i), min_layer_value);
- }
- }
- }
- TEST_F(LayerCache, FillAndClearCache) {
- fill_cache();
- clear_cache();
- for (int i=0; i < MATRIX_ROWS; i++) {
- for (int j=0; j < MATRIX_COLS; j++) {
- EXPECT_EQ(read_cache(j, i), min_layer_value);
- }
- }
- }
- TEST_F(LayerCache, WriteAndReadFirstPosMaximumValue) {
- write_cache(0, 0, max_layer_value);
- EXPECT_EQ(read_cache(0, 0), max_layer_value);
- // The second position should not be updated
- EXPECT_EQ(read_cache(1, 0), min_layer_value);
- EXPECT_EQ(read_cache(0, 1), min_layer_value);
- }
- TEST_F(LayerCache, WriteAndReadSecondPosMaximumValue) {
- write_cache(1, 0, max_layer_value);
- EXPECT_EQ(read_cache(1, 0), max_layer_value);
- // The surrounding positions should not be updated
- EXPECT_EQ(read_cache(0, 0), min_layer_value);
- EXPECT_EQ(read_cache(0, 1), min_layer_value);
- }
- TEST_F(LayerCache, WriteAndReadFirstPosAlternatingBitsStartingWith1) {
- write_cache(0, 0, alternating_starting_with_1);
- EXPECT_EQ(read_cache(0, 0), alternating_starting_with_1);
- // The second position should not be updated
- EXPECT_EQ(read_cache(1, 0), min_layer_value);
- EXPECT_EQ(read_cache(0, 1), min_layer_value);
- }
- TEST_F(LayerCache, WriteAndReadSecondPosAlternatingBitsStartingWith1) {
- write_cache(1, 0, alternating_starting_with_1);
- EXPECT_EQ(read_cache(1, 0), alternating_starting_with_1);
- // The surrounding positions should not be updated
- EXPECT_EQ(read_cache(0, 0), min_layer_value);
- EXPECT_EQ(read_cache(0, 1), min_layer_value);
- }
- TEST_F(LayerCache, WriteAndReadFirstPosAlternatingBitsStartingWith0) {
- write_cache(0, 0, alternating_starting_with_0);
- EXPECT_EQ(read_cache(0, 0), alternating_starting_with_0);
- // The second position should not be updated
- EXPECT_EQ(read_cache(1, 0), min_layer_value);
- EXPECT_EQ(read_cache(0, 1), min_layer_value);
- }
- TEST_F(LayerCache, WriteAndReadSecondPosAlternatingBitsStartingWith0) {
- write_cache(1, 0, alternating_starting_with_0);
- EXPECT_EQ(read_cache(1, 0), alternating_starting_with_0);
- // The surrounding positions should not be updated
- EXPECT_EQ(read_cache(0, 0), min_layer_value);
- EXPECT_EQ(read_cache(0, 1), min_layer_value);
- }
|