sat_counter.test.cc revision 13962
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
3313962Sodanrc@yahoo.com.br#include <utility>
3413962Sodanrc@yahoo.com.br
3513961Sodanrc@yahoo.com.br#include "base/sat_counter.hh"
3613961Sodanrc@yahoo.com.br
3713961Sodanrc@yahoo.com.br/**
3813961Sodanrc@yahoo.com.br * Test if the maximum value is indeed the maximum value reachable.
3913961Sodanrc@yahoo.com.br */
4013961Sodanrc@yahoo.com.brTEST(SatCounterTest, MaximumValue)
4113961Sodanrc@yahoo.com.br{
4213961Sodanrc@yahoo.com.br    const unsigned bits = 3;
4313961Sodanrc@yahoo.com.br    const unsigned max_value = (1 << bits) - 1;
4413961Sodanrc@yahoo.com.br    SatCounter counter(bits);
4513961Sodanrc@yahoo.com.br
4613961Sodanrc@yahoo.com.br    for (int i = 0; i < 2*max_value; i++) {
4713961Sodanrc@yahoo.com.br        counter++;
4813961Sodanrc@yahoo.com.br    }
4913961Sodanrc@yahoo.com.br
5013961Sodanrc@yahoo.com.br    ASSERT_EQ(counter, max_value);
5113961Sodanrc@yahoo.com.br}
5213961Sodanrc@yahoo.com.br
5313961Sodanrc@yahoo.com.br/**
5413961Sodanrc@yahoo.com.br * Test if the minimum value is indeed the mimimum value reachable.
5513961Sodanrc@yahoo.com.br */
5613961Sodanrc@yahoo.com.brTEST(SatCounterTest, MinimumValue)
5713961Sodanrc@yahoo.com.br{
5813961Sodanrc@yahoo.com.br    const unsigned bits = 3;
5913961Sodanrc@yahoo.com.br    SatCounter counter(bits);
6013961Sodanrc@yahoo.com.br
6113961Sodanrc@yahoo.com.br    for (int i = 0; i < 2; i++) {
6213961Sodanrc@yahoo.com.br        counter--;
6313961Sodanrc@yahoo.com.br    }
6413961Sodanrc@yahoo.com.br
6513961Sodanrc@yahoo.com.br    ASSERT_EQ(counter, 0);
6613961Sodanrc@yahoo.com.br}
6713961Sodanrc@yahoo.com.br
6813961Sodanrc@yahoo.com.br/**
6913961Sodanrc@yahoo.com.br * Test initializing the counter with a value, updating it and then resetting.
7013961Sodanrc@yahoo.com.br */
7113961Sodanrc@yahoo.com.brTEST(SatCounterTest, InitialValue)
7213961Sodanrc@yahoo.com.br{
7313961Sodanrc@yahoo.com.br    const unsigned bits = 3;
7413961Sodanrc@yahoo.com.br    const unsigned initial_value = 4;
7513961Sodanrc@yahoo.com.br    SatCounter counter(bits, initial_value);
7613961Sodanrc@yahoo.com.br    ASSERT_EQ(counter, initial_value);
7713961Sodanrc@yahoo.com.br    counter++;
7813961Sodanrc@yahoo.com.br    counter.reset();
7913961Sodanrc@yahoo.com.br    ASSERT_EQ(counter, initial_value);
8013961Sodanrc@yahoo.com.br}
8113961Sodanrc@yahoo.com.br
8213961Sodanrc@yahoo.com.br/**
8313962Sodanrc@yahoo.com.br * Test calculating saturation percentile.
8413962Sodanrc@yahoo.com.br */
8513962Sodanrc@yahoo.com.brTEST(SatCounterTest, SaturationPercentile)
8613962Sodanrc@yahoo.com.br{
8713962Sodanrc@yahoo.com.br    const unsigned bits = 3;
8813962Sodanrc@yahoo.com.br    const unsigned max_value = (1 << bits) - 1;
8913962Sodanrc@yahoo.com.br    SatCounter counter(bits);
9013962Sodanrc@yahoo.com.br
9113962Sodanrc@yahoo.com.br    ASSERT_FALSE(counter.isSaturated());
9213962Sodanrc@yahoo.com.br    for (double value = 0.0; value <= max_value; value++, counter++) {
9313962Sodanrc@yahoo.com.br        const double saturation = value / max_value;
9413962Sodanrc@yahoo.com.br        ASSERT_DOUBLE_EQ(counter.calcSaturation(), saturation);
9513962Sodanrc@yahoo.com.br    }
9613962Sodanrc@yahoo.com.br    ASSERT_TRUE(counter.isSaturated());
9713962Sodanrc@yahoo.com.br}
9813962Sodanrc@yahoo.com.br
9913962Sodanrc@yahoo.com.br/**
10013961Sodanrc@yahoo.com.br * Test back and forth against an int.
10113961Sodanrc@yahoo.com.br */
10213961Sodanrc@yahoo.com.brTEST(SatCounterTest, IntComparison)
10313961Sodanrc@yahoo.com.br{
10413961Sodanrc@yahoo.com.br    const unsigned bits = 3;
10513961Sodanrc@yahoo.com.br    SatCounter counter(bits);
10613961Sodanrc@yahoo.com.br    int value = 0;
10713961Sodanrc@yahoo.com.br
10813961Sodanrc@yahoo.com.br    ASSERT_EQ(counter++, value++);
10913961Sodanrc@yahoo.com.br    ASSERT_EQ(counter++, value++);
11013961Sodanrc@yahoo.com.br    ASSERT_EQ(counter--, value--);
11113961Sodanrc@yahoo.com.br    ASSERT_EQ(counter++, value++);
11213961Sodanrc@yahoo.com.br    ASSERT_EQ(counter++, value++);
11313961Sodanrc@yahoo.com.br    ASSERT_EQ(counter--, value--);
11413961Sodanrc@yahoo.com.br    ASSERT_EQ(counter++, value++);
11513961Sodanrc@yahoo.com.br    ASSERT_EQ(counter--, value--);
11613961Sodanrc@yahoo.com.br    ASSERT_EQ(counter--, value--);
11713961Sodanrc@yahoo.com.br    ASSERT_EQ(counter++, value++);
11813961Sodanrc@yahoo.com.br    ASSERT_EQ(counter--, value--);
11913961Sodanrc@yahoo.com.br    ASSERT_EQ(counter--, value--);
12013961Sodanrc@yahoo.com.br    ASSERT_EQ(counter, 0);
12113961Sodanrc@yahoo.com.br}
12213961Sodanrc@yahoo.com.br
12313961Sodanrc@yahoo.com.br/**
12413962Sodanrc@yahoo.com.br * Test shift operators.
12513962Sodanrc@yahoo.com.br */
12613962Sodanrc@yahoo.com.brTEST(SatCounterTest, Shift)
12713962Sodanrc@yahoo.com.br{
12813962Sodanrc@yahoo.com.br    const unsigned bits = 3;
12913962Sodanrc@yahoo.com.br    const unsigned max_value = (1 << bits) - 1;
13013962Sodanrc@yahoo.com.br    const unsigned initial_value = 1;
13113962Sodanrc@yahoo.com.br    SatCounter counter(bits, initial_value);
13213962Sodanrc@yahoo.com.br    SatCounter other(bits, initial_value);
13313962Sodanrc@yahoo.com.br    // The saturated shift value is just enough to saturate, since greater
13413962Sodanrc@yahoo.com.br    // values could generate undefined behavior
13513962Sodanrc@yahoo.com.br    SatCounter saturated_counter(bits, bits);
13613962Sodanrc@yahoo.com.br    int value = initial_value;
13713962Sodanrc@yahoo.com.br
13813962Sodanrc@yahoo.com.br    // Test random shifts
13913962Sodanrc@yahoo.com.br    counter <<= 2;
14013962Sodanrc@yahoo.com.br    value <<= 2;
14113962Sodanrc@yahoo.com.br    ASSERT_EQ(counter, value);
14213962Sodanrc@yahoo.com.br    counter >>= 1;
14313962Sodanrc@yahoo.com.br    value >>= 1;
14413962Sodanrc@yahoo.com.br    ASSERT_EQ(counter, value);
14513962Sodanrc@yahoo.com.br
14613962Sodanrc@yahoo.com.br    // Test saturation
14713962Sodanrc@yahoo.com.br    counter <<= bits;
14813962Sodanrc@yahoo.com.br    ASSERT_EQ(counter, max_value);
14913962Sodanrc@yahoo.com.br
15013962Sodanrc@yahoo.com.br    // Test zeroing
15113962Sodanrc@yahoo.com.br    counter >>= bits;
15213962Sodanrc@yahoo.com.br    ASSERT_EQ(counter, 0);
15313962Sodanrc@yahoo.com.br
15413962Sodanrc@yahoo.com.br    // Test saturation against other saturating counter
15513962Sodanrc@yahoo.com.br    counter.reset();
15613962Sodanrc@yahoo.com.br    value = initial_value;
15713962Sodanrc@yahoo.com.br    counter <<= other;
15813962Sodanrc@yahoo.com.br    value <<= other;
15913962Sodanrc@yahoo.com.br    ASSERT_EQ(counter, value);
16013962Sodanrc@yahoo.com.br    counter <<= saturated_counter;
16113962Sodanrc@yahoo.com.br    value = max_value;
16213962Sodanrc@yahoo.com.br    ASSERT_EQ(counter, max_value);
16313962Sodanrc@yahoo.com.br
16413962Sodanrc@yahoo.com.br    // Test zeroing against other saturating counter
16513962Sodanrc@yahoo.com.br    counter >>= other;
16613962Sodanrc@yahoo.com.br    value >>= other;
16713962Sodanrc@yahoo.com.br    ASSERT_EQ(counter, value);
16813962Sodanrc@yahoo.com.br    counter >>= saturated_counter;
16913962Sodanrc@yahoo.com.br    ASSERT_EQ(counter, 0);
17013962Sodanrc@yahoo.com.br}
17113962Sodanrc@yahoo.com.br
17213962Sodanrc@yahoo.com.br/**
17313961Sodanrc@yahoo.com.br * Test both pre and post operators.
17413961Sodanrc@yahoo.com.br */
17513961Sodanrc@yahoo.com.brTEST(SatCounterTest, PrePostOperators)
17613961Sodanrc@yahoo.com.br{
17713961Sodanrc@yahoo.com.br    const unsigned bits = 3;
17813961Sodanrc@yahoo.com.br    const unsigned max_value = (1 << bits) - 1;
17913961Sodanrc@yahoo.com.br    SatCounter counter_pre(bits);
18013961Sodanrc@yahoo.com.br    SatCounter counter_post(bits);
18113961Sodanrc@yahoo.com.br
18213961Sodanrc@yahoo.com.br    for (int i = 0; i < 2*max_value; i++) {
18313961Sodanrc@yahoo.com.br        counter_post++;
18413961Sodanrc@yahoo.com.br        SatCounter value_pre = ++counter_pre;
18513961Sodanrc@yahoo.com.br        ASSERT_EQ(counter_post, value_pre);
18613961Sodanrc@yahoo.com.br    }
18713961Sodanrc@yahoo.com.br
18813961Sodanrc@yahoo.com.br    ASSERT_EQ(counter_pre, max_value);
18913961Sodanrc@yahoo.com.br    ASSERT_EQ(counter_post, max_value);
19013961Sodanrc@yahoo.com.br
19113961Sodanrc@yahoo.com.br    for (int i = 0; i < 2*max_value; i++) {
19213961Sodanrc@yahoo.com.br        counter_post--;
19313961Sodanrc@yahoo.com.br        SatCounter value_pre = --counter_pre;
19413961Sodanrc@yahoo.com.br        ASSERT_EQ(counter_post, value_pre);
19513961Sodanrc@yahoo.com.br    }
19613961Sodanrc@yahoo.com.br
19713961Sodanrc@yahoo.com.br    ASSERT_EQ(counter_pre, 0);
19813961Sodanrc@yahoo.com.br    ASSERT_EQ(counter_post, 0);
19913961Sodanrc@yahoo.com.br}
20013962Sodanrc@yahoo.com.br
20113962Sodanrc@yahoo.com.br/**
20213962Sodanrc@yahoo.com.br * Test copy and move for both constructor and assignment.
20313962Sodanrc@yahoo.com.br */
20413962Sodanrc@yahoo.com.brTEST(SatCounterTest, CopyMove)
20513962Sodanrc@yahoo.com.br{
20613962Sodanrc@yahoo.com.br    const unsigned bits = 3;
20713962Sodanrc@yahoo.com.br    const unsigned max_value = (1 << bits) - 1;
20813962Sodanrc@yahoo.com.br    const unsigned initial_value = 1;
20913962Sodanrc@yahoo.com.br    SatCounter counter(bits, initial_value);
21013962Sodanrc@yahoo.com.br    SatCounter deep_copy(1);
21113962Sodanrc@yahoo.com.br    SatCounter counter_copy(2);
21213962Sodanrc@yahoo.com.br
21313962Sodanrc@yahoo.com.br    // Increase counter value so that we can check if the inner counter is
21413962Sodanrc@yahoo.com.br    // being copied
21513962Sodanrc@yahoo.com.br    counter++;
21613962Sodanrc@yahoo.com.br
21713962Sodanrc@yahoo.com.br    // Copy counter using both the copy constructor and the copy assignment
21813962Sodanrc@yahoo.com.br    SatCounter counter_copy_constructor(counter);
21913962Sodanrc@yahoo.com.br    deep_copy = counter_copy = counter;
22013962Sodanrc@yahoo.com.br    ASSERT_EQ(counter_copy_constructor, initial_value + 1);
22113962Sodanrc@yahoo.com.br    ASSERT_EQ(counter_copy, initial_value + 1);
22213962Sodanrc@yahoo.com.br    ASSERT_EQ(deep_copy, initial_value + 1);
22313962Sodanrc@yahoo.com.br
22413962Sodanrc@yahoo.com.br    // Make sure max value is the same for all of them, and that modifying
22513962Sodanrc@yahoo.com.br    // the copies does not modify the original
22613962Sodanrc@yahoo.com.br    for (int i = 0; i < 2*max_value; i++) {
22713962Sodanrc@yahoo.com.br        counter_copy_constructor++;
22813962Sodanrc@yahoo.com.br        counter_copy++;
22913962Sodanrc@yahoo.com.br        deep_copy++;
23013962Sodanrc@yahoo.com.br    }
23113962Sodanrc@yahoo.com.br    ASSERT_EQ(counter, initial_value + 1);
23213962Sodanrc@yahoo.com.br    ASSERT_EQ(counter_copy_constructor, max_value);
23313962Sodanrc@yahoo.com.br    ASSERT_EQ(counter_copy, max_value);
23413962Sodanrc@yahoo.com.br    ASSERT_EQ(deep_copy, max_value);
23513962Sodanrc@yahoo.com.br
23613962Sodanrc@yahoo.com.br    // Make sure initial value is the same for all of them
23713962Sodanrc@yahoo.com.br    counter_copy_constructor.reset();
23813962Sodanrc@yahoo.com.br    counter_copy.reset();
23913962Sodanrc@yahoo.com.br    deep_copy.reset();
24013962Sodanrc@yahoo.com.br    ASSERT_EQ(counter_copy_constructor, initial_value);
24113962Sodanrc@yahoo.com.br    ASSERT_EQ(counter_copy, initial_value);
24213962Sodanrc@yahoo.com.br    ASSERT_EQ(deep_copy, initial_value);
24313962Sodanrc@yahoo.com.br
24413962Sodanrc@yahoo.com.br    // Now check move
24513962Sodanrc@yahoo.com.br    SatCounter counter_move_constructor(std::move(counter));
24613962Sodanrc@yahoo.com.br    ASSERT_EQ(counter, 0);
24713962Sodanrc@yahoo.com.br    ASSERT_EQ(counter_move_constructor, initial_value + 1);
24813962Sodanrc@yahoo.com.br
24913962Sodanrc@yahoo.com.br    SatCounter counter_move(bits);
25013962Sodanrc@yahoo.com.br    counter_move = std::move(counter_move_constructor);
25113962Sodanrc@yahoo.com.br    ASSERT_EQ(counter_move_constructor, 0);
25213962Sodanrc@yahoo.com.br    ASSERT_EQ(counter_move, initial_value + 1);
25313962Sodanrc@yahoo.com.br}
25413962Sodanrc@yahoo.com.br
25513962Sodanrc@yahoo.com.br/**
25613962Sodanrc@yahoo.com.br * Test add-assignment and subtract assignment.
25713962Sodanrc@yahoo.com.br */
25813962Sodanrc@yahoo.com.brTEST(SatCounterTest, AddSubAssignment)
25913962Sodanrc@yahoo.com.br{
26013962Sodanrc@yahoo.com.br    const unsigned bits = 3;
26113962Sodanrc@yahoo.com.br    const unsigned max_value = (1 << bits) - 1;
26213962Sodanrc@yahoo.com.br    SatCounter counter(bits);
26313962Sodanrc@yahoo.com.br    SatCounter other(bits, 2);
26413962Sodanrc@yahoo.com.br    SatCounter saturated_counter(bits, max_value);
26513962Sodanrc@yahoo.com.br    int value = 0;
26613962Sodanrc@yahoo.com.br
26713962Sodanrc@yahoo.com.br    // Test add-assignment for a few random values and then saturate
26813962Sodanrc@yahoo.com.br    counter += 2;
26913962Sodanrc@yahoo.com.br    value += 2;
27013962Sodanrc@yahoo.com.br    ASSERT_EQ(counter, value);
27113962Sodanrc@yahoo.com.br    counter += 3;
27213962Sodanrc@yahoo.com.br    value += 3;
27313962Sodanrc@yahoo.com.br    ASSERT_EQ(counter, value);
27413962Sodanrc@yahoo.com.br    counter += max_value;
27513962Sodanrc@yahoo.com.br    value = max_value;
27613962Sodanrc@yahoo.com.br    ASSERT_EQ(counter, value);
27713962Sodanrc@yahoo.com.br
27813962Sodanrc@yahoo.com.br    // Test subtract-assignment for a few random values until back to zero
27913962Sodanrc@yahoo.com.br    counter -= 2;
28013962Sodanrc@yahoo.com.br    value -= 2;
28113962Sodanrc@yahoo.com.br    ASSERT_EQ(counter, value);
28213962Sodanrc@yahoo.com.br    counter -= 3;
28313962Sodanrc@yahoo.com.br    value -= 3;
28413962Sodanrc@yahoo.com.br    ASSERT_EQ(counter, value);
28513962Sodanrc@yahoo.com.br    counter -= max_value;
28613962Sodanrc@yahoo.com.br    value = 0;
28713962Sodanrc@yahoo.com.br    ASSERT_EQ(counter, value);
28813962Sodanrc@yahoo.com.br
28913962Sodanrc@yahoo.com.br    // Test add-assignment of other saturating counter
29013962Sodanrc@yahoo.com.br    counter += other;
29113962Sodanrc@yahoo.com.br    value += other;
29213962Sodanrc@yahoo.com.br    ASSERT_EQ(counter, value);
29313962Sodanrc@yahoo.com.br    counter += saturated_counter;
29413962Sodanrc@yahoo.com.br    value = max_value;
29513962Sodanrc@yahoo.com.br    ASSERT_EQ(counter, saturated_counter);
29613962Sodanrc@yahoo.com.br
29713962Sodanrc@yahoo.com.br    // Test subtract-assignment of other saturating counter
29813962Sodanrc@yahoo.com.br    counter -= other;
29913962Sodanrc@yahoo.com.br    value -= other;
30013962Sodanrc@yahoo.com.br    ASSERT_EQ(counter, value);
30113962Sodanrc@yahoo.com.br    counter -= saturated_counter;
30213962Sodanrc@yahoo.com.br    ASSERT_EQ(counter, 0);
30313962Sodanrc@yahoo.com.br}
30413962Sodanrc@yahoo.com.br
305