2 * Copyright (c) 2019 Inria
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * Authors: Daniel Carvalho
31 #include <gtest/gtest.h>
33 #include "base/sat_counter.hh"
36 * Test if the maximum value is indeed the maximum value reachable.
38 TEST(SatCounterTest
, MaximumValue
)
40 const unsigned bits
= 3;
41 const unsigned max_value
= (1 << bits
) - 1;
42 SatCounter
counter(bits
);
44 for (int i
= 0; i
< 2*max_value
; i
++) {
48 ASSERT_EQ(counter
, max_value
);
52 * Test if the minimum value is indeed the mimimum value reachable.
54 TEST(SatCounterTest
, MinimumValue
)
56 const unsigned bits
= 3;
57 SatCounter
counter(bits
);
59 for (int i
= 0; i
< 2; i
++) {
63 ASSERT_EQ(counter
, 0);
67 * Test initializing the counter with a value, updating it and then resetting.
69 TEST(SatCounterTest
, InitialValue
)
71 const unsigned bits
= 3;
72 const unsigned initial_value
= 4;
73 SatCounter
counter(bits
, initial_value
);
74 ASSERT_EQ(counter
, initial_value
);
77 ASSERT_EQ(counter
, initial_value
);
81 * Test back and forth against an int.
83 TEST(SatCounterTest
, IntComparison
)
85 const unsigned bits
= 3;
86 SatCounter
counter(bits
);
89 ASSERT_EQ(counter
++, value
++);
90 ASSERT_EQ(counter
++, value
++);
91 ASSERT_EQ(counter
--, value
--);
92 ASSERT_EQ(counter
++, value
++);
93 ASSERT_EQ(counter
++, value
++);
94 ASSERT_EQ(counter
--, value
--);
95 ASSERT_EQ(counter
++, value
++);
96 ASSERT_EQ(counter
--, value
--);
97 ASSERT_EQ(counter
--, value
--);
98 ASSERT_EQ(counter
++, value
++);
99 ASSERT_EQ(counter
--, value
--);
100 ASSERT_EQ(counter
--, value
--);
101 ASSERT_EQ(counter
, 0);
105 * Test both pre and post operators.
107 TEST(SatCounterTest
, PrePostOperators
)
109 const unsigned bits
= 3;
110 const unsigned max_value
= (1 << bits
) - 1;
111 SatCounter
counter_pre(bits
);
112 SatCounter
counter_post(bits
);
114 for (int i
= 0; i
< 2*max_value
; i
++) {
116 SatCounter value_pre
= ++counter_pre
;
117 ASSERT_EQ(counter_post
, value_pre
);
120 ASSERT_EQ(counter_pre
, max_value
);
121 ASSERT_EQ(counter_post
, max_value
);
123 for (int i
= 0; i
< 2*max_value
; i
++) {
125 SatCounter value_pre
= --counter_pre
;
126 ASSERT_EQ(counter_post
, value_pre
);
129 ASSERT_EQ(counter_pre
, 0);
130 ASSERT_EQ(counter_post
, 0);