1 /* sane - Scanner Access Now Easy.
2 
3    Copyright (C) 2019 Povilas Kanapickas <povilas@radix.lt>
4 
5    This file is part of the SANE package.
6 
7    This program is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License as
9    published by the Free Software Foundation; either version 2 of the
10    License, or (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <https://www.gnu.org/licenses/>.
19 */
20 
21 #define DEBUG_DECLARE_ONLY
22 
23 #include "tests.h"
24 #include "minigtest.h"
25 #include "tests_printers.h"
26 
27 #include "../../../backend/genesys/low.h"
28 
29 #include <numeric>
30 
31 namespace genesys {
32 
test_row_buffer_push_pop_forward(unsigned size)33 void test_row_buffer_push_pop_forward(unsigned size)
34 {
35     RowBuffer buf{1};
36 
37     ASSERT_TRUE(buf.empty());
38     for (unsigned i = 0; i < size; i++) {
39         buf.push_back();
40         *buf.get_back_row_ptr() = i;
41         for (unsigned j = 0; j < i + 1; j++) {
42             ASSERT_EQ(*buf.get_row_ptr(j), j);
43         }
44     }
45     ASSERT_FALSE(buf.empty());
46 
47     for (unsigned i = 0; i < 10; i++) {
48         ASSERT_EQ(buf.height(), size);
49         ASSERT_EQ(static_cast<unsigned>(*buf.get_front_row_ptr()), i);
50         buf.pop_front();
51         ASSERT_EQ(buf.height(), size - 1);
52         buf.push_back();
53         *buf.get_back_row_ptr() = i + size;
54     }
55 }
56 
test_row_buffer_push_pop_backward(unsigned size)57 void test_row_buffer_push_pop_backward(unsigned size)
58 {
59     RowBuffer buf{1};
60 
61     ASSERT_TRUE(buf.empty());
62     for (unsigned i = 0; i < size; i++) {
63         buf.push_front();
64         *buf.get_front_row_ptr() = i;
65         for (unsigned j = 0; j < i + 1; j++) {
66             ASSERT_EQ(*buf.get_row_ptr(j), i - j);
67         }
68     }
69     ASSERT_FALSE(buf.empty());
70 
71     for (unsigned i = 0; i < 10; i++) {
72         ASSERT_EQ(buf.height(), size);
73         ASSERT_EQ(static_cast<unsigned>(*buf.get_back_row_ptr()), i);
74         buf.pop_back();
75         ASSERT_EQ(buf.height(), size - 1);
76         buf.push_front();
77         *buf.get_front_row_ptr() = i + size;
78     }
79 }
80 
test_row_buffer()81 void test_row_buffer()
82 {
83     for (unsigned size = 1; size < 5; ++size) {
84         test_row_buffer_push_pop_forward(size);
85         test_row_buffer_push_pop_backward(size);
86     }
87 }
88 
89 } // namespace genesys
90