base: Add GTest to SatCounter
[gem5.git] / src / base / sat_counter.test.cc
1 /*
2 * Copyright (c) 2019 Inria
3 * All rights reserved
4 *
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.
15 *
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.
27 *
28 * Authors: Daniel Carvalho
29 */
30
31 #include <gtest/gtest.h>
32
33 #include "base/sat_counter.hh"
34
35 /**
36 * Test if the maximum value is indeed the maximum value reachable.
37 */
38 TEST(SatCounterTest, MaximumValue)
39 {
40 const unsigned bits = 3;
41 const unsigned max_value = (1 << bits) - 1;
42 SatCounter counter(bits);
43
44 for (int i = 0; i < 2*max_value; i++) {
45 counter++;
46 }
47
48 ASSERT_EQ(counter, max_value);
49 }
50
51 /**
52 * Test if the minimum value is indeed the mimimum value reachable.
53 */
54 TEST(SatCounterTest, MinimumValue)
55 {
56 const unsigned bits = 3;
57 SatCounter counter(bits);
58
59 for (int i = 0; i < 2; i++) {
60 counter--;
61 }
62
63 ASSERT_EQ(counter, 0);
64 }
65
66 /**
67 * Test initializing the counter with a value, updating it and then resetting.
68 */
69 TEST(SatCounterTest, InitialValue)
70 {
71 const unsigned bits = 3;
72 const unsigned initial_value = 4;
73 SatCounter counter(bits, initial_value);
74 ASSERT_EQ(counter, initial_value);
75 counter++;
76 counter.reset();
77 ASSERT_EQ(counter, initial_value);
78 }
79
80 /**
81 * Test back and forth against an int.
82 */
83 TEST(SatCounterTest, IntComparison)
84 {
85 const unsigned bits = 3;
86 SatCounter counter(bits);
87 int value = 0;
88
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);
102 }
103
104 /**
105 * Test both pre and post operators.
106 */
107 TEST(SatCounterTest, PrePostOperators)
108 {
109 const unsigned bits = 3;
110 const unsigned max_value = (1 << bits) - 1;
111 SatCounter counter_pre(bits);
112 SatCounter counter_post(bits);
113
114 for (int i = 0; i < 2*max_value; i++) {
115 counter_post++;
116 SatCounter value_pre = ++counter_pre;
117 ASSERT_EQ(counter_post, value_pre);
118 }
119
120 ASSERT_EQ(counter_pre, max_value);
121 ASSERT_EQ(counter_post, max_value);
122
123 for (int i = 0; i < 2*max_value; i++) {
124 counter_post--;
125 SatCounter value_pre = --counter_pre;
126 ASSERT_EQ(counter_post, value_pre);
127 }
128
129 ASSERT_EQ(counter_pre, 0);
130 ASSERT_EQ(counter_post, 0);
131 }