sat_counter.test.cc revision 14210
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/** 10014210Sodanrc@yahoo.com.br * Test abrupt saturation. 10114210Sodanrc@yahoo.com.br */ 10214210Sodanrc@yahoo.com.brTEST(SatCounterTest, Saturate) 10314210Sodanrc@yahoo.com.br{ 10414210Sodanrc@yahoo.com.br const unsigned bits = 3; 10514210Sodanrc@yahoo.com.br const unsigned max_value = (1 << bits) - 1; 10614210Sodanrc@yahoo.com.br SatCounter counter(bits); 10714210Sodanrc@yahoo.com.br counter++; 10814210Sodanrc@yahoo.com.br ASSERT_FALSE(counter.isSaturated()); 10914210Sodanrc@yahoo.com.br 11014210Sodanrc@yahoo.com.br // Make sure the value added is what was missing to saturate 11114210Sodanrc@yahoo.com.br const unsigned diff = counter.saturate(); 11214210Sodanrc@yahoo.com.br ASSERT_EQ(diff, max_value - 1); 11314210Sodanrc@yahoo.com.br ASSERT_TRUE(counter.isSaturated()); 11414210Sodanrc@yahoo.com.br} 11514210Sodanrc@yahoo.com.br 11614210Sodanrc@yahoo.com.br/** 11713961Sodanrc@yahoo.com.br * Test back and forth against an int. 11813961Sodanrc@yahoo.com.br */ 11913961Sodanrc@yahoo.com.brTEST(SatCounterTest, IntComparison) 12013961Sodanrc@yahoo.com.br{ 12113961Sodanrc@yahoo.com.br const unsigned bits = 3; 12213961Sodanrc@yahoo.com.br SatCounter counter(bits); 12313961Sodanrc@yahoo.com.br int value = 0; 12413961Sodanrc@yahoo.com.br 12513961Sodanrc@yahoo.com.br ASSERT_EQ(counter++, value++); 12613961Sodanrc@yahoo.com.br ASSERT_EQ(counter++, value++); 12713961Sodanrc@yahoo.com.br ASSERT_EQ(counter--, value--); 12813961Sodanrc@yahoo.com.br ASSERT_EQ(counter++, value++); 12913961Sodanrc@yahoo.com.br ASSERT_EQ(counter++, value++); 13013961Sodanrc@yahoo.com.br ASSERT_EQ(counter--, value--); 13113961Sodanrc@yahoo.com.br ASSERT_EQ(counter++, value++); 13213961Sodanrc@yahoo.com.br ASSERT_EQ(counter--, value--); 13313961Sodanrc@yahoo.com.br ASSERT_EQ(counter--, value--); 13413961Sodanrc@yahoo.com.br ASSERT_EQ(counter++, value++); 13513961Sodanrc@yahoo.com.br ASSERT_EQ(counter--, value--); 13613961Sodanrc@yahoo.com.br ASSERT_EQ(counter--, value--); 13713961Sodanrc@yahoo.com.br ASSERT_EQ(counter, 0); 13813961Sodanrc@yahoo.com.br} 13913961Sodanrc@yahoo.com.br 14013961Sodanrc@yahoo.com.br/** 14113962Sodanrc@yahoo.com.br * Test shift operators. 14213962Sodanrc@yahoo.com.br */ 14313962Sodanrc@yahoo.com.brTEST(SatCounterTest, Shift) 14413962Sodanrc@yahoo.com.br{ 14513962Sodanrc@yahoo.com.br const unsigned bits = 3; 14613962Sodanrc@yahoo.com.br const unsigned max_value = (1 << bits) - 1; 14713962Sodanrc@yahoo.com.br const unsigned initial_value = 1; 14813962Sodanrc@yahoo.com.br SatCounter counter(bits, initial_value); 14913962Sodanrc@yahoo.com.br SatCounter other(bits, initial_value); 15013962Sodanrc@yahoo.com.br // The saturated shift value is just enough to saturate, since greater 15113962Sodanrc@yahoo.com.br // values could generate undefined behavior 15213962Sodanrc@yahoo.com.br SatCounter saturated_counter(bits, bits); 15313962Sodanrc@yahoo.com.br int value = initial_value; 15413962Sodanrc@yahoo.com.br 15513962Sodanrc@yahoo.com.br // Test random shifts 15613962Sodanrc@yahoo.com.br counter <<= 2; 15713962Sodanrc@yahoo.com.br value <<= 2; 15813962Sodanrc@yahoo.com.br ASSERT_EQ(counter, value); 15913962Sodanrc@yahoo.com.br counter >>= 1; 16013962Sodanrc@yahoo.com.br value >>= 1; 16113962Sodanrc@yahoo.com.br ASSERT_EQ(counter, value); 16213962Sodanrc@yahoo.com.br 16313962Sodanrc@yahoo.com.br // Test saturation 16413962Sodanrc@yahoo.com.br counter <<= bits; 16513962Sodanrc@yahoo.com.br ASSERT_EQ(counter, max_value); 16613962Sodanrc@yahoo.com.br 16713962Sodanrc@yahoo.com.br // Test zeroing 16813962Sodanrc@yahoo.com.br counter >>= bits; 16913962Sodanrc@yahoo.com.br ASSERT_EQ(counter, 0); 17013962Sodanrc@yahoo.com.br 17113962Sodanrc@yahoo.com.br // Test saturation against other saturating counter 17213962Sodanrc@yahoo.com.br counter.reset(); 17313962Sodanrc@yahoo.com.br value = initial_value; 17413962Sodanrc@yahoo.com.br counter <<= other; 17513962Sodanrc@yahoo.com.br value <<= other; 17613962Sodanrc@yahoo.com.br ASSERT_EQ(counter, value); 17713962Sodanrc@yahoo.com.br counter <<= saturated_counter; 17813962Sodanrc@yahoo.com.br value = max_value; 17913962Sodanrc@yahoo.com.br ASSERT_EQ(counter, max_value); 18013962Sodanrc@yahoo.com.br 18113962Sodanrc@yahoo.com.br // Test zeroing against other saturating counter 18213962Sodanrc@yahoo.com.br counter >>= other; 18313962Sodanrc@yahoo.com.br value >>= other; 18413962Sodanrc@yahoo.com.br ASSERT_EQ(counter, value); 18513962Sodanrc@yahoo.com.br counter >>= saturated_counter; 18613962Sodanrc@yahoo.com.br ASSERT_EQ(counter, 0); 18713962Sodanrc@yahoo.com.br} 18813962Sodanrc@yahoo.com.br 18913962Sodanrc@yahoo.com.br/** 19013961Sodanrc@yahoo.com.br * Test both pre and post operators. 19113961Sodanrc@yahoo.com.br */ 19213961Sodanrc@yahoo.com.brTEST(SatCounterTest, PrePostOperators) 19313961Sodanrc@yahoo.com.br{ 19413961Sodanrc@yahoo.com.br const unsigned bits = 3; 19513961Sodanrc@yahoo.com.br const unsigned max_value = (1 << bits) - 1; 19613961Sodanrc@yahoo.com.br SatCounter counter_pre(bits); 19713961Sodanrc@yahoo.com.br SatCounter counter_post(bits); 19813961Sodanrc@yahoo.com.br 19913961Sodanrc@yahoo.com.br for (int i = 0; i < 2*max_value; i++) { 20013961Sodanrc@yahoo.com.br counter_post++; 20113961Sodanrc@yahoo.com.br SatCounter value_pre = ++counter_pre; 20213961Sodanrc@yahoo.com.br ASSERT_EQ(counter_post, value_pre); 20313961Sodanrc@yahoo.com.br } 20413961Sodanrc@yahoo.com.br 20513961Sodanrc@yahoo.com.br ASSERT_EQ(counter_pre, max_value); 20613961Sodanrc@yahoo.com.br ASSERT_EQ(counter_post, max_value); 20713961Sodanrc@yahoo.com.br 20813961Sodanrc@yahoo.com.br for (int i = 0; i < 2*max_value; i++) { 20913961Sodanrc@yahoo.com.br counter_post--; 21013961Sodanrc@yahoo.com.br SatCounter value_pre = --counter_pre; 21113961Sodanrc@yahoo.com.br ASSERT_EQ(counter_post, value_pre); 21213961Sodanrc@yahoo.com.br } 21313961Sodanrc@yahoo.com.br 21413961Sodanrc@yahoo.com.br ASSERT_EQ(counter_pre, 0); 21513961Sodanrc@yahoo.com.br ASSERT_EQ(counter_post, 0); 21613961Sodanrc@yahoo.com.br} 21713962Sodanrc@yahoo.com.br 21813962Sodanrc@yahoo.com.br/** 21913962Sodanrc@yahoo.com.br * Test copy and move for both constructor and assignment. 22013962Sodanrc@yahoo.com.br */ 22113962Sodanrc@yahoo.com.brTEST(SatCounterTest, CopyMove) 22213962Sodanrc@yahoo.com.br{ 22313962Sodanrc@yahoo.com.br const unsigned bits = 3; 22413962Sodanrc@yahoo.com.br const unsigned max_value = (1 << bits) - 1; 22513962Sodanrc@yahoo.com.br const unsigned initial_value = 1; 22613962Sodanrc@yahoo.com.br SatCounter counter(bits, initial_value); 22713962Sodanrc@yahoo.com.br SatCounter deep_copy(1); 22813962Sodanrc@yahoo.com.br SatCounter counter_copy(2); 22913962Sodanrc@yahoo.com.br 23013962Sodanrc@yahoo.com.br // Increase counter value so that we can check if the inner counter is 23113962Sodanrc@yahoo.com.br // being copied 23213962Sodanrc@yahoo.com.br counter++; 23313962Sodanrc@yahoo.com.br 23413962Sodanrc@yahoo.com.br // Copy counter using both the copy constructor and the copy assignment 23513962Sodanrc@yahoo.com.br SatCounter counter_copy_constructor(counter); 23613962Sodanrc@yahoo.com.br deep_copy = counter_copy = counter; 23713962Sodanrc@yahoo.com.br ASSERT_EQ(counter_copy_constructor, initial_value + 1); 23813962Sodanrc@yahoo.com.br ASSERT_EQ(counter_copy, initial_value + 1); 23913962Sodanrc@yahoo.com.br ASSERT_EQ(deep_copy, initial_value + 1); 24013962Sodanrc@yahoo.com.br 24113962Sodanrc@yahoo.com.br // Make sure max value is the same for all of them, and that modifying 24213962Sodanrc@yahoo.com.br // the copies does not modify the original 24313962Sodanrc@yahoo.com.br for (int i = 0; i < 2*max_value; i++) { 24413962Sodanrc@yahoo.com.br counter_copy_constructor++; 24513962Sodanrc@yahoo.com.br counter_copy++; 24613962Sodanrc@yahoo.com.br deep_copy++; 24713962Sodanrc@yahoo.com.br } 24813962Sodanrc@yahoo.com.br ASSERT_EQ(counter, initial_value + 1); 24913962Sodanrc@yahoo.com.br ASSERT_EQ(counter_copy_constructor, max_value); 25013962Sodanrc@yahoo.com.br ASSERT_EQ(counter_copy, max_value); 25113962Sodanrc@yahoo.com.br ASSERT_EQ(deep_copy, max_value); 25213962Sodanrc@yahoo.com.br 25313962Sodanrc@yahoo.com.br // Make sure initial value is the same for all of them 25413962Sodanrc@yahoo.com.br counter_copy_constructor.reset(); 25513962Sodanrc@yahoo.com.br counter_copy.reset(); 25613962Sodanrc@yahoo.com.br deep_copy.reset(); 25713962Sodanrc@yahoo.com.br ASSERT_EQ(counter_copy_constructor, initial_value); 25813962Sodanrc@yahoo.com.br ASSERT_EQ(counter_copy, initial_value); 25913962Sodanrc@yahoo.com.br ASSERT_EQ(deep_copy, initial_value); 26013962Sodanrc@yahoo.com.br 26113962Sodanrc@yahoo.com.br // Now check move 26213962Sodanrc@yahoo.com.br SatCounter counter_move_constructor(std::move(counter)); 26313962Sodanrc@yahoo.com.br ASSERT_EQ(counter, 0); 26413962Sodanrc@yahoo.com.br ASSERT_EQ(counter_move_constructor, initial_value + 1); 26513962Sodanrc@yahoo.com.br 26613962Sodanrc@yahoo.com.br SatCounter counter_move(bits); 26713962Sodanrc@yahoo.com.br counter_move = std::move(counter_move_constructor); 26813962Sodanrc@yahoo.com.br ASSERT_EQ(counter_move_constructor, 0); 26913962Sodanrc@yahoo.com.br ASSERT_EQ(counter_move, initial_value + 1); 27013962Sodanrc@yahoo.com.br} 27113962Sodanrc@yahoo.com.br 27213962Sodanrc@yahoo.com.br/** 27313962Sodanrc@yahoo.com.br * Test add-assignment and subtract assignment. 27413962Sodanrc@yahoo.com.br */ 27513962Sodanrc@yahoo.com.brTEST(SatCounterTest, AddSubAssignment) 27613962Sodanrc@yahoo.com.br{ 27713962Sodanrc@yahoo.com.br const unsigned bits = 3; 27813962Sodanrc@yahoo.com.br const unsigned max_value = (1 << bits) - 1; 27913962Sodanrc@yahoo.com.br SatCounter counter(bits); 28013962Sodanrc@yahoo.com.br SatCounter other(bits, 2); 28113962Sodanrc@yahoo.com.br SatCounter saturated_counter(bits, max_value); 28213962Sodanrc@yahoo.com.br int value = 0; 28313962Sodanrc@yahoo.com.br 28413962Sodanrc@yahoo.com.br // Test add-assignment for a few random values and then saturate 28513962Sodanrc@yahoo.com.br counter += 2; 28613962Sodanrc@yahoo.com.br value += 2; 28713962Sodanrc@yahoo.com.br ASSERT_EQ(counter, value); 28813962Sodanrc@yahoo.com.br counter += 3; 28913962Sodanrc@yahoo.com.br value += 3; 29013962Sodanrc@yahoo.com.br ASSERT_EQ(counter, value); 29113962Sodanrc@yahoo.com.br counter += max_value; 29213962Sodanrc@yahoo.com.br value = max_value; 29313962Sodanrc@yahoo.com.br ASSERT_EQ(counter, value); 29413962Sodanrc@yahoo.com.br 29513962Sodanrc@yahoo.com.br // Test subtract-assignment for a few random values until back to zero 29613962Sodanrc@yahoo.com.br counter -= 2; 29713962Sodanrc@yahoo.com.br value -= 2; 29813962Sodanrc@yahoo.com.br ASSERT_EQ(counter, value); 29913962Sodanrc@yahoo.com.br counter -= 3; 30013962Sodanrc@yahoo.com.br value -= 3; 30113962Sodanrc@yahoo.com.br ASSERT_EQ(counter, value); 30213962Sodanrc@yahoo.com.br counter -= max_value; 30313962Sodanrc@yahoo.com.br value = 0; 30413962Sodanrc@yahoo.com.br ASSERT_EQ(counter, value); 30513962Sodanrc@yahoo.com.br 30613962Sodanrc@yahoo.com.br // Test add-assignment of other saturating counter 30713962Sodanrc@yahoo.com.br counter += other; 30813962Sodanrc@yahoo.com.br value += other; 30913962Sodanrc@yahoo.com.br ASSERT_EQ(counter, value); 31013962Sodanrc@yahoo.com.br counter += saturated_counter; 31113962Sodanrc@yahoo.com.br value = max_value; 31213962Sodanrc@yahoo.com.br ASSERT_EQ(counter, saturated_counter); 31313962Sodanrc@yahoo.com.br 31413962Sodanrc@yahoo.com.br // Test subtract-assignment of other saturating counter 31513962Sodanrc@yahoo.com.br counter -= other; 31613962Sodanrc@yahoo.com.br value -= other; 31713962Sodanrc@yahoo.com.br ASSERT_EQ(counter, value); 31813962Sodanrc@yahoo.com.br counter -= saturated_counter; 31913962Sodanrc@yahoo.com.br ASSERT_EQ(counter, 0); 32013962Sodanrc@yahoo.com.br} 32113962Sodanrc@yahoo.com.br 322