sat_counter.test.cc revision 13961
113961Sodanrc@yahoo.com.br/*
213961Sodanrc@yahoo.com.br * Copyright (c) 2019 Inria
313961Sodanrc@yahoo.com.br * All rights reserved
413961Sodanrc@yahoo.com.br *
513961Sodanrc@yahoo.com.br * Redistribution and use in source and binary forms, with or without
613961Sodanrc@yahoo.com.br * modification, are permitted provided that the following conditions are
713961Sodanrc@yahoo.com.br * met: redistributions of source code must retain the above copyright
813961Sodanrc@yahoo.com.br * notice, this list of conditions and the following disclaimer;
913961Sodanrc@yahoo.com.br * redistributions in binary form must reproduce the above copyright
1013961Sodanrc@yahoo.com.br * notice, this list of conditions and the following disclaimer in the
1113961Sodanrc@yahoo.com.br * documentation and/or other materials provided with the distribution;
1213961Sodanrc@yahoo.com.br * neither the name of the copyright holders nor the names of its
1313961Sodanrc@yahoo.com.br * contributors may be used to endorse or promote products derived from
1413961Sodanrc@yahoo.com.br * this software without specific prior written permission.
1513961Sodanrc@yahoo.com.br *
1613961Sodanrc@yahoo.com.br * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1713961Sodanrc@yahoo.com.br * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1813961Sodanrc@yahoo.com.br * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1913961Sodanrc@yahoo.com.br * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2013961Sodanrc@yahoo.com.br * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2113961Sodanrc@yahoo.com.br * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2213961Sodanrc@yahoo.com.br * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2313961Sodanrc@yahoo.com.br * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2413961Sodanrc@yahoo.com.br * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2513961Sodanrc@yahoo.com.br * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2613961Sodanrc@yahoo.com.br * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2713961Sodanrc@yahoo.com.br *
2813961Sodanrc@yahoo.com.br * Authors: Daniel Carvalho
2913961Sodanrc@yahoo.com.br */
3013961Sodanrc@yahoo.com.br
3113961Sodanrc@yahoo.com.br#include <gtest/gtest.h>
3213961Sodanrc@yahoo.com.br
3313961Sodanrc@yahoo.com.br#include "base/sat_counter.hh"
3413961Sodanrc@yahoo.com.br
3513961Sodanrc@yahoo.com.br/**
3613961Sodanrc@yahoo.com.br * Test if the maximum value is indeed the maximum value reachable.
3713961Sodanrc@yahoo.com.br */
3813961Sodanrc@yahoo.com.brTEST(SatCounterTest, MaximumValue)
3913961Sodanrc@yahoo.com.br{
4013961Sodanrc@yahoo.com.br    const unsigned bits = 3;
4113961Sodanrc@yahoo.com.br    const unsigned max_value = (1 << bits) - 1;
4213961Sodanrc@yahoo.com.br    SatCounter counter(bits);
4313961Sodanrc@yahoo.com.br
4413961Sodanrc@yahoo.com.br    for (int i = 0; i < 2*max_value; i++) {
4513961Sodanrc@yahoo.com.br        counter++;
4613961Sodanrc@yahoo.com.br    }
4713961Sodanrc@yahoo.com.br
4813961Sodanrc@yahoo.com.br    ASSERT_EQ(counter, max_value);
4913961Sodanrc@yahoo.com.br}
5013961Sodanrc@yahoo.com.br
5113961Sodanrc@yahoo.com.br/**
5213961Sodanrc@yahoo.com.br * Test if the minimum value is indeed the mimimum value reachable.
5313961Sodanrc@yahoo.com.br */
5413961Sodanrc@yahoo.com.brTEST(SatCounterTest, MinimumValue)
5513961Sodanrc@yahoo.com.br{
5613961Sodanrc@yahoo.com.br    const unsigned bits = 3;
5713961Sodanrc@yahoo.com.br    SatCounter counter(bits);
5813961Sodanrc@yahoo.com.br
5913961Sodanrc@yahoo.com.br    for (int i = 0; i < 2; i++) {
6013961Sodanrc@yahoo.com.br        counter--;
6113961Sodanrc@yahoo.com.br    }
6213961Sodanrc@yahoo.com.br
6313961Sodanrc@yahoo.com.br    ASSERT_EQ(counter, 0);
6413961Sodanrc@yahoo.com.br}
6513961Sodanrc@yahoo.com.br
6613961Sodanrc@yahoo.com.br/**
6713961Sodanrc@yahoo.com.br * Test initializing the counter with a value, updating it and then resetting.
6813961Sodanrc@yahoo.com.br */
6913961Sodanrc@yahoo.com.brTEST(SatCounterTest, InitialValue)
7013961Sodanrc@yahoo.com.br{
7113961Sodanrc@yahoo.com.br    const unsigned bits = 3;
7213961Sodanrc@yahoo.com.br    const unsigned initial_value = 4;
7313961Sodanrc@yahoo.com.br    SatCounter counter(bits, initial_value);
7413961Sodanrc@yahoo.com.br    ASSERT_EQ(counter, initial_value);
7513961Sodanrc@yahoo.com.br    counter++;
7613961Sodanrc@yahoo.com.br    counter.reset();
7713961Sodanrc@yahoo.com.br    ASSERT_EQ(counter, initial_value);
7813961Sodanrc@yahoo.com.br}
7913961Sodanrc@yahoo.com.br
8013961Sodanrc@yahoo.com.br/**
8113961Sodanrc@yahoo.com.br * Test back and forth against an int.
8213961Sodanrc@yahoo.com.br */
8313961Sodanrc@yahoo.com.brTEST(SatCounterTest, IntComparison)
8413961Sodanrc@yahoo.com.br{
8513961Sodanrc@yahoo.com.br    const unsigned bits = 3;
8613961Sodanrc@yahoo.com.br    SatCounter counter(bits);
8713961Sodanrc@yahoo.com.br    int value = 0;
8813961Sodanrc@yahoo.com.br
8913961Sodanrc@yahoo.com.br    ASSERT_EQ(counter++, value++);
9013961Sodanrc@yahoo.com.br    ASSERT_EQ(counter++, value++);
9113961Sodanrc@yahoo.com.br    ASSERT_EQ(counter--, value--);
9213961Sodanrc@yahoo.com.br    ASSERT_EQ(counter++, value++);
9313961Sodanrc@yahoo.com.br    ASSERT_EQ(counter++, value++);
9413961Sodanrc@yahoo.com.br    ASSERT_EQ(counter--, value--);
9513961Sodanrc@yahoo.com.br    ASSERT_EQ(counter++, value++);
9613961Sodanrc@yahoo.com.br    ASSERT_EQ(counter--, value--);
9713961Sodanrc@yahoo.com.br    ASSERT_EQ(counter--, value--);
9813961Sodanrc@yahoo.com.br    ASSERT_EQ(counter++, value++);
9913961Sodanrc@yahoo.com.br    ASSERT_EQ(counter--, value--);
10013961Sodanrc@yahoo.com.br    ASSERT_EQ(counter--, value--);
10113961Sodanrc@yahoo.com.br    ASSERT_EQ(counter, 0);
10213961Sodanrc@yahoo.com.br}
10313961Sodanrc@yahoo.com.br
10413961Sodanrc@yahoo.com.br/**
10513961Sodanrc@yahoo.com.br * Test both pre and post operators.
10613961Sodanrc@yahoo.com.br */
10713961Sodanrc@yahoo.com.brTEST(SatCounterTest, PrePostOperators)
10813961Sodanrc@yahoo.com.br{
10913961Sodanrc@yahoo.com.br    const unsigned bits = 3;
11013961Sodanrc@yahoo.com.br    const unsigned max_value = (1 << bits) - 1;
11113961Sodanrc@yahoo.com.br    SatCounter counter_pre(bits);
11213961Sodanrc@yahoo.com.br    SatCounter counter_post(bits);
11313961Sodanrc@yahoo.com.br
11413961Sodanrc@yahoo.com.br    for (int i = 0; i < 2*max_value; i++) {
11513961Sodanrc@yahoo.com.br        counter_post++;
11613961Sodanrc@yahoo.com.br        SatCounter value_pre = ++counter_pre;
11713961Sodanrc@yahoo.com.br        ASSERT_EQ(counter_post, value_pre);
11813961Sodanrc@yahoo.com.br    }
11913961Sodanrc@yahoo.com.br
12013961Sodanrc@yahoo.com.br    ASSERT_EQ(counter_pre, max_value);
12113961Sodanrc@yahoo.com.br    ASSERT_EQ(counter_post, max_value);
12213961Sodanrc@yahoo.com.br
12313961Sodanrc@yahoo.com.br    for (int i = 0; i < 2*max_value; i++) {
12413961Sodanrc@yahoo.com.br        counter_post--;
12513961Sodanrc@yahoo.com.br        SatCounter value_pre = --counter_pre;
12613961Sodanrc@yahoo.com.br        ASSERT_EQ(counter_post, value_pre);
12713961Sodanrc@yahoo.com.br    }
12813961Sodanrc@yahoo.com.br
12913961Sodanrc@yahoo.com.br    ASSERT_EQ(counter_pre, 0);
13013961Sodanrc@yahoo.com.br    ASSERT_EQ(counter_post, 0);
13113961Sodanrc@yahoo.com.br}
132