TestRollingNumber.cpp (3258B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include <gtest/gtest.h> 8 9 #include <cstdint> 10 #include <type_traits> 11 12 #include "RollingNumber.h" 13 14 using RN8 = mozilla::RollingNumber<uint8_t>; 15 16 TEST(RollingNumber, Value) 17 { 18 // Value type should reflect template argument. 19 static_assert(std::is_same_v<RN8::ValueType, uint8_t>); 20 21 // Default init to 0. 22 const RN8 n; 23 // Access through Value(). 24 EXPECT_EQ(0, n.Value()); 25 26 // Conversion constructor. 27 RN8 n42{42}; 28 EXPECT_EQ(42, n42.Value()); 29 30 // Copy Constructor. 31 RN8 n42Copied{n42}; 32 EXPECT_EQ(42, n42Copied.Value()); 33 34 // Assignment construction. 35 RN8 n42Assigned = n42; 36 EXPECT_EQ(42, n42Assigned.Value()); 37 38 // Assignment. 39 n42 = n; 40 EXPECT_EQ(0, n42.Value()); 41 } 42 43 TEST(RollingNumber, Operations) 44 { 45 RN8 n; 46 EXPECT_EQ(0, n.Value()); 47 48 RN8 nPreInc = ++n; 49 EXPECT_EQ(1, n.Value()); 50 EXPECT_EQ(1, nPreInc.Value()); 51 52 RN8 nPostInc = n++; 53 EXPECT_EQ(2, n.Value()); 54 EXPECT_EQ(1, nPostInc.Value()); 55 56 RN8 nPreDec = --n; 57 EXPECT_EQ(1, n.Value()); 58 EXPECT_EQ(1, nPreDec.Value()); 59 60 RN8 nPostDec = n--; 61 EXPECT_EQ(0, n.Value()); 62 EXPECT_EQ(1, nPostDec.Value()); 63 64 RN8 nPlus = n + 10; 65 EXPECT_EQ(0, n.Value()); 66 EXPECT_EQ(10, nPlus.Value()); 67 68 n += 20; 69 EXPECT_EQ(20, n.Value()); 70 71 RN8 nMinus = n - 2; 72 EXPECT_EQ(20, n.Value()); 73 EXPECT_EQ(18, nMinus.Value()); 74 75 n -= 5; 76 EXPECT_EQ(15, n.Value()); 77 78 uint8_t diff = nMinus - n; 79 EXPECT_EQ(3, diff); 80 81 // Overflows. 82 n = RN8(0); 83 EXPECT_EQ(0, n.Value()); 84 n--; 85 EXPECT_EQ(255, n.Value()); 86 n++; 87 EXPECT_EQ(0, n.Value()); 88 n -= 10; 89 EXPECT_EQ(246, n.Value()); 90 n += 20; 91 EXPECT_EQ(10, n.Value()); 92 } 93 94 TEST(RollingNumber, Comparisons) 95 { 96 uint8_t i = 0; 97 do { 98 RN8 n{i}; 99 EXPECT_EQ(i, n.Value()); 100 EXPECT_TRUE(n == n); 101 EXPECT_FALSE(n != n); 102 EXPECT_FALSE(n < n); 103 EXPECT_TRUE(n <= n); 104 EXPECT_FALSE(n > n); 105 EXPECT_TRUE(n >= n); 106 107 RN8 same = n; 108 EXPECT_TRUE(n == same); 109 EXPECT_FALSE(n != same); 110 EXPECT_FALSE(n < same); 111 EXPECT_TRUE(n <= same); 112 EXPECT_FALSE(n > same); 113 EXPECT_TRUE(n >= same); 114 115 #ifdef DEBUG 116 // In debug builds, we are only allowed a quarter of the type range. 117 const uint8_t maxDiff = 255 / 4; 118 #else 119 // In non-debug builds, we can go half-way up or down the type range, and 120 // still conserve the expected ordering. 121 const uint8_t maxDiff = 255 / 2; 122 #endif 123 for (uint8_t add = 1; add <= maxDiff; ++add) { 124 RN8 bigger = n + add; 125 EXPECT_FALSE(n == bigger); 126 EXPECT_TRUE(n != bigger); 127 EXPECT_TRUE(n < bigger); 128 EXPECT_TRUE(n <= bigger); 129 EXPECT_FALSE(n > bigger); 130 EXPECT_FALSE(n >= bigger); 131 } 132 133 for (uint8_t sub = 1; sub <= maxDiff; ++sub) { 134 RN8 smaller = n - sub; 135 EXPECT_FALSE(n == smaller); 136 EXPECT_TRUE(n != smaller); 137 EXPECT_FALSE(n < smaller); 138 EXPECT_FALSE(n <= smaller); 139 EXPECT_TRUE(n > smaller); 140 EXPECT_TRUE(n >= smaller); 141 } 142 143 ++i; 144 } while (i != 0); 145 }