sat_counter.test.cc revision 13961:3139b24cd76d
110810Sbr@bsdpad.com/*
210810Sbr@bsdpad.com * Copyright (c) 2019 Inria
310810Sbr@bsdpad.com * All rights reserved
410810Sbr@bsdpad.com *
510810Sbr@bsdpad.com * Redistribution and use in source and binary forms, with or without
610810Sbr@bsdpad.com * modification, are permitted provided that the following conditions are
710810Sbr@bsdpad.com * met: redistributions of source code must retain the above copyright
810810Sbr@bsdpad.com * notice, this list of conditions and the following disclaimer;
910810Sbr@bsdpad.com * redistributions in binary form must reproduce the above copyright
1010810Sbr@bsdpad.com * notice, this list of conditions and the following disclaimer in the
1110810Sbr@bsdpad.com * documentation and/or other materials provided with the distribution;
1210810Sbr@bsdpad.com * neither the name of the copyright holders nor the names of its
1310810Sbr@bsdpad.com * contributors may be used to endorse or promote products derived from
1410810Sbr@bsdpad.com * this software without specific prior written permission.
1510810Sbr@bsdpad.com *
1610810Sbr@bsdpad.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1710810Sbr@bsdpad.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1810810Sbr@bsdpad.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1910810Sbr@bsdpad.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2010810Sbr@bsdpad.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2110810Sbr@bsdpad.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2210810Sbr@bsdpad.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2310810Sbr@bsdpad.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2410810Sbr@bsdpad.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2510810Sbr@bsdpad.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2610810Sbr@bsdpad.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2710810Sbr@bsdpad.com *
2810810Sbr@bsdpad.com * Authors: Daniel Carvalho
2910810Sbr@bsdpad.com */
3010810Sbr@bsdpad.com
3110810Sbr@bsdpad.com#include <gtest/gtest.h>
3210810Sbr@bsdpad.com
3310810Sbr@bsdpad.com#include "base/sat_counter.hh"
3410810Sbr@bsdpad.com
3510810Sbr@bsdpad.com/**
3610810Sbr@bsdpad.com * Test if the maximum value is indeed the maximum value reachable.
3710810Sbr@bsdpad.com */
3810810Sbr@bsdpad.comTEST(SatCounterTest, MaximumValue)
3910810Sbr@bsdpad.com{
4010810Sbr@bsdpad.com    const unsigned bits = 3;
4110810Sbr@bsdpad.com    const unsigned max_value = (1 << bits) - 1;
4210810Sbr@bsdpad.com    SatCounter counter(bits);
4310810Sbr@bsdpad.com
4410810Sbr@bsdpad.com    for (int i = 0; i < 2*max_value; i++) {
4510810Sbr@bsdpad.com        counter++;
4610810Sbr@bsdpad.com    }
4710810Sbr@bsdpad.com
4810810Sbr@bsdpad.com    ASSERT_EQ(counter, max_value);
4910810Sbr@bsdpad.com}
5010810Sbr@bsdpad.com
5110810Sbr@bsdpad.com/**
5210810Sbr@bsdpad.com * Test if the minimum value is indeed the mimimum value reachable.
5310810Sbr@bsdpad.com */
5410810Sbr@bsdpad.comTEST(SatCounterTest, MinimumValue)
5510810Sbr@bsdpad.com{
5610810Sbr@bsdpad.com    const unsigned bits = 3;
5710810Sbr@bsdpad.com    SatCounter counter(bits);
5810810Sbr@bsdpad.com
5910810Sbr@bsdpad.com    for (int i = 0; i < 2; i++) {
6010810Sbr@bsdpad.com        counter--;
6110810Sbr@bsdpad.com    }
6210810Sbr@bsdpad.com
6310810Sbr@bsdpad.com    ASSERT_EQ(counter, 0);
6410810Sbr@bsdpad.com}
6510810Sbr@bsdpad.com
6610810Sbr@bsdpad.com/**
6710810Sbr@bsdpad.com * Test initializing the counter with a value, updating it and then resetting.
6810810Sbr@bsdpad.com */
69TEST(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 */
83TEST(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 */
107TEST(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}
132