LCOV - code coverage report
Current view: directory - xpcom/tests - TestCheckedInt.cpp (source / functions) Found Hit Coverage
Test: app.info Lines: 290 280 96.6 %
Date: 2012-06-02 Functions: 21 20 95.2 %

       1                 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
       2                 : /* vim:set ts=2 sw=2 sts=2 et cindent: */
       3                 : /* ***** BEGIN LICENSE BLOCK *****
       4                 :  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
       5                 :  *
       6                 :  * The contents of this file are subject to the Mozilla Public License Version
       7                 :  * 1.1 (the "License"); you may not use this file except in compliance with
       8                 :  * the License. You may obtain a copy of the License at
       9                 :  * http://www.mozilla.org/MPL/
      10                 :  *
      11                 :  * Software distributed under the License is distributed on an "AS IS" basis,
      12                 :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
      13                 :  * for the specific language governing rights and limitations under the
      14                 :  * License.
      15                 :  *
      16                 :  * The Original Code is Mozilla code.
      17                 :  *
      18                 :  * The Initial Developer of the Original Code is the Mozilla Corporation.
      19                 :  * Portions created by the Initial Developer are Copyright (C) 2009
      20                 :  * the Initial Developer. All Rights Reserved.
      21                 :  *
      22                 :  * Contributor(s):
      23                 :  *  Benoit Jacob <bjacob@mozilla.com>
      24                 :  *
      25                 :  * Alternatively, the contents of this file may be used under the terms of
      26                 :  * either the GNU General Public License Version 2 or later (the "GPL"), or
      27                 :  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
      28                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
      29                 :  * of those above. If you wish to allow use of your version of this file only
      30                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
      31                 :  * use your version of this file under the terms of the MPL, indicate your
      32                 :  * decision by deleting the provisions above and replace them with the notice
      33                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
      34                 :  * the provisions above, a recipient may use your version of this file under
      35                 :  * the terms of any one of the MPL, the GPL or the LGPL.
      36                 :  *
      37                 :  * ***** END LICENSE BLOCK ***** */
      38                 : 
      39                 : 
      40                 : #include "CheckedInt.h"
      41                 : #include <iostream>
      42                 : 
      43                 : namespace CheckedInt_test {
      44                 : 
      45                 : using namespace mozilla::CheckedInt_internal;
      46                 : using mozilla::CheckedInt;
      47                 : 
      48                 : int g_tests_passed = 0;
      49                 : int g_tests_failed = 0;
      50                 : 
      51            1968 : void verify_impl_function(bool x, bool expected,
      52                 :                           const char* file, int line,
      53                 :                           int T_size, bool T_is_signed)
      54                 : {
      55            1968 :     if (x == expected) {
      56            1968 :         g_tests_passed++;
      57                 :     } else {
      58               0 :         g_tests_failed++;
      59               0 :         std::cerr << "Test failed at " << file << ":" << line;
      60               0 :         std::cerr << " with T a ";
      61               0 :         if(T_is_signed)
      62               0 :             std::cerr << "signed";
      63                 :         else
      64               0 :             std::cerr << "unsigned";
      65               0 :         std::cerr << " " << CHAR_BIT*T_size << "-bit integer type" << std::endl;
      66                 :     }
      67            1968 : }
      68                 : 
      69                 : #define VERIFY_IMPL(x, expected) \
      70                 :     verify_impl_function((x), (expected), __FILE__, __LINE__, sizeof(T), integer_traits<T>::is_signed)
      71                 : 
      72                 : #define VERIFY(x)            VERIFY_IMPL(x, true)
      73                 : #define VERIFY_IS_FALSE(x)   VERIFY_IMPL(x, false)
      74                 : #define VERIFY_IS_VALID(x)   VERIFY_IMPL((x).valid(), true)
      75                 : #define VERIFY_IS_INVALID(x) VERIFY_IMPL((x).valid(), false)
      76                 : #define VERIFY_IS_VALID_IF(x,condition) VERIFY_IMPL((x).valid(), (condition))
      77                 : 
      78                 : template<typename T, unsigned int size = sizeof(T)>
      79                 : struct test_twice_bigger_type
      80                 : {
      81               6 :     static void run()
      82                 :     {
      83               6 :         VERIFY(integer_traits<T>::twice_bigger_type_is_supported);
      84               6 :         VERIFY(sizeof(typename integer_traits<T>::twice_bigger_type)
      85                 :                     == 2 * sizeof(T));
      86               6 :         VERIFY(bool(integer_traits<
      87                 :                     typename integer_traits<T>::twice_bigger_type
      88                 :                 >::is_signed) == bool(integer_traits<T>::is_signed));
      89               6 :     }
      90                 : };
      91                 : 
      92                 : template<typename T>
      93                 : struct test_twice_bigger_type<T, 8>
      94                 : {
      95               2 :     static void run()
      96                 :     {
      97               2 :         VERIFY_IS_FALSE(integer_traits<T>::twice_bigger_type_is_supported);
      98               2 :     }
      99                 : };
     100                 : 
     101                 : 
     102                 : template<typename T>
     103               8 : void test()
     104                 : {
     105                 :     static bool already_run = false;
     106               8 :     if (already_run) {
     107               0 :         g_tests_failed++;
     108               0 :         std::cerr << "You already tested this type. Copy/paste typo??" << std::endl;
     109               0 :         return;
     110                 :     }
     111               8 :     already_run = true;
     112                 : 
     113               8 :     VERIFY(integer_traits<T>::is_supported);
     114               8 :     VERIFY(integer_traits<T>::size == sizeof(T));
     115                 :     enum{ is_signed = integer_traits<T>::is_signed };
     116               8 :     VERIFY(bool(is_signed) == !bool(T(-1) > T(0)));
     117                 : 
     118               8 :     test_twice_bigger_type<T>::run();
     119                 : 
     120                 :     typedef typename integer_traits<T>::unsigned_type unsigned_T;
     121                 : 
     122               8 :     VERIFY(sizeof(unsigned_T) == sizeof(T));
     123               8 :     VERIFY(integer_traits<unsigned_T>::is_signed == false);
     124                 : 
     125               8 :     CheckedInt<T> max_value(integer_traits<T>::max_value());
     126               8 :     CheckedInt<T> min_value(integer_traits<T>::min_value());
     127                 : 
     128                 :     // check min_value() and max_value(), since they are custom implementations and a mistake there
     129                 :     // could potentially NOT be caught by any other tests... while making everything wrong!
     130                 : 
     131               8 :     T bit = 1;
     132             240 :     for(unsigned int i = 0; i < sizeof(T) * CHAR_BIT - 1; i++)
     133                 :     {
     134             232 :         VERIFY((min_value.value() & bit) == 0);
     135             232 :         bit <<= 1;
     136                 :     }
     137               8 :     VERIFY((min_value.value() & bit) == (is_signed ? bit : T(0)));
     138               8 :     VERIFY(max_value.value() == T(~(min_value.value())));
     139                 : 
     140               8 :     CheckedInt<T> zero(0);
     141               8 :     CheckedInt<T> one(1);
     142               8 :     CheckedInt<T> two(2);
     143               8 :     CheckedInt<T> three(3);
     144               8 :     CheckedInt<T> four(4);
     145                 : 
     146                 :     /* addition / substraction checks */
     147                 : 
     148               8 :     VERIFY_IS_VALID(zero+zero);
     149               8 :     VERIFY(zero+zero == zero);
     150               8 :     VERIFY_IS_FALSE(zero+zero == one); // check that == doesn't always return true
     151               8 :     VERIFY_IS_VALID(zero+one);
     152               8 :     VERIFY(zero+one == one);
     153               8 :     VERIFY_IS_VALID(one+one);
     154               8 :     VERIFY(one+one == two);
     155                 : 
     156               8 :     CheckedInt<T> max_value_minus_one = max_value - one;
     157               8 :     CheckedInt<T> max_value_minus_two = max_value - two;
     158               8 :     VERIFY_IS_VALID(max_value_minus_one);
     159               8 :     VERIFY_IS_VALID(max_value_minus_two);
     160               8 :     VERIFY_IS_VALID(max_value_minus_one + one);
     161               8 :     VERIFY_IS_VALID(max_value_minus_two + one);
     162               8 :     VERIFY_IS_VALID(max_value_minus_two + two);
     163               8 :     VERIFY(max_value_minus_one + one == max_value);
     164               8 :     VERIFY(max_value_minus_two + one == max_value_minus_one);
     165               8 :     VERIFY(max_value_minus_two + two == max_value);
     166                 : 
     167               8 :     VERIFY_IS_VALID(max_value + zero);
     168               8 :     VERIFY_IS_VALID(max_value - zero);
     169               8 :     VERIFY_IS_INVALID(max_value + one);
     170               8 :     VERIFY_IS_INVALID(max_value + two);
     171               8 :     VERIFY_IS_INVALID(max_value + max_value_minus_one);
     172               8 :     VERIFY_IS_INVALID(max_value + max_value);
     173                 : 
     174               8 :     CheckedInt<T> min_value_plus_one = min_value + one;
     175               8 :     CheckedInt<T> min_value_plus_two = min_value + two;
     176               8 :     VERIFY_IS_VALID(min_value_plus_one);
     177               8 :     VERIFY_IS_VALID(min_value_plus_two);
     178               8 :     VERIFY_IS_VALID(min_value_plus_one - one);
     179               8 :     VERIFY_IS_VALID(min_value_plus_two - one);
     180               8 :     VERIFY_IS_VALID(min_value_plus_two - two);
     181               8 :     VERIFY(min_value_plus_one - one == min_value);
     182               8 :     VERIFY(min_value_plus_two - one == min_value_plus_one);
     183               8 :     VERIFY(min_value_plus_two - two == min_value);
     184                 : 
     185               8 :     CheckedInt<T> min_value_minus_one = min_value - one;
     186               8 :     VERIFY_IS_VALID(min_value + zero);
     187               8 :     VERIFY_IS_VALID(min_value - zero);
     188               8 :     VERIFY_IS_INVALID(min_value - one);
     189               8 :     VERIFY_IS_INVALID(min_value - two);
     190               8 :     VERIFY_IS_INVALID(min_value - min_value_minus_one);
     191               8 :     VERIFY_IS_VALID(min_value - min_value);
     192                 : 
     193               8 :     CheckedInt<T> max_value_over_two = max_value / two;
     194               8 :     VERIFY_IS_VALID(max_value_over_two + max_value_over_two);
     195               8 :     VERIFY_IS_VALID(max_value_over_two + one);
     196               8 :     VERIFY((max_value_over_two + one) - one == max_value_over_two);
     197               8 :     VERIFY_IS_VALID(max_value_over_two - max_value_over_two);
     198               8 :     VERIFY(max_value_over_two - max_value_over_two == zero);
     199                 : 
     200               8 :     CheckedInt<T> min_value_over_two = min_value / two;
     201               8 :     VERIFY_IS_VALID(min_value_over_two + min_value_over_two);
     202               8 :     VERIFY_IS_VALID(min_value_over_two + one);
     203               8 :     VERIFY((min_value_over_two + one) - one == min_value_over_two);
     204               8 :     VERIFY_IS_VALID(min_value_over_two - min_value_over_two);
     205               8 :     VERIFY(min_value_over_two - min_value_over_two == zero);
     206                 : 
     207               8 :     VERIFY_IS_INVALID(min_value - one);
     208               8 :     VERIFY_IS_INVALID(min_value - two);
     209                 : 
     210                 :     if (is_signed) {
     211               4 :         VERIFY_IS_INVALID(min_value + min_value);
     212               4 :         VERIFY_IS_INVALID(min_value_over_two + min_value_over_two + min_value_over_two);
     213               4 :         VERIFY_IS_INVALID(zero - min_value + min_value);
     214               4 :         VERIFY_IS_INVALID(one - min_value + min_value);
     215                 :     }
     216                 : 
     217                 :     /* unary operator- checks */
     218                 : 
     219               8 :     CheckedInt<T> neg_one = -one;
     220               8 :     CheckedInt<T> neg_two = -two;
     221                 : 
     222                 :     if (is_signed) {
     223               4 :         VERIFY_IS_VALID(-max_value);
     224               4 :         VERIFY_IS_VALID(-max_value - one);
     225               4 :         VERIFY_IS_VALID(neg_one);
     226               4 :         VERIFY_IS_VALID(-max_value + neg_one);
     227               4 :         VERIFY_IS_VALID(neg_one + one);
     228               4 :         VERIFY(neg_one + one == zero);
     229               4 :         VERIFY_IS_VALID(neg_two);
     230               4 :         VERIFY_IS_VALID(neg_one + neg_one);
     231               4 :         VERIFY(neg_one + neg_one == neg_two);
     232                 :     } else {
     233               4 :         VERIFY_IS_INVALID(neg_one);
     234                 :     }
     235                 : 
     236                 :     /* multiplication checks */
     237                 : 
     238               8 :     VERIFY_IS_VALID(zero*zero);
     239               8 :     VERIFY(zero*zero == zero);
     240               8 :     VERIFY_IS_VALID(zero*one);
     241               8 :     VERIFY(zero*one == zero);
     242               8 :     VERIFY_IS_VALID(one*zero);
     243               8 :     VERIFY(one*zero == zero);
     244               8 :     VERIFY_IS_VALID(one*one);
     245               8 :     VERIFY(one*one == one);
     246               8 :     VERIFY_IS_VALID(one*three);
     247               8 :     VERIFY(one*three == three);
     248               8 :     VERIFY_IS_VALID(two*two);
     249               8 :     VERIFY(two*two == four);
     250                 : 
     251               8 :     VERIFY_IS_INVALID(max_value * max_value);
     252               8 :     VERIFY_IS_INVALID(max_value_over_two * max_value);
     253               8 :     VERIFY_IS_INVALID(max_value_over_two * max_value_over_two);
     254                 : 
     255               8 :     CheckedInt<T> max_value_approx_sqrt(T(T(1) << (CHAR_BIT*sizeof(T)/2)));
     256                 : 
     257               8 :     VERIFY_IS_VALID(max_value_approx_sqrt);
     258               8 :     VERIFY_IS_VALID(max_value_approx_sqrt * two);
     259               8 :     VERIFY_IS_INVALID(max_value_approx_sqrt * max_value_approx_sqrt);
     260               8 :     VERIFY_IS_INVALID(max_value_approx_sqrt * max_value_approx_sqrt * max_value_approx_sqrt);
     261                 : 
     262                 :     if (is_signed) {
     263               4 :         VERIFY_IS_INVALID(min_value * min_value);
     264               4 :         VERIFY_IS_INVALID(min_value_over_two * min_value);
     265               4 :         VERIFY_IS_INVALID(min_value_over_two * min_value_over_two);
     266                 : 
     267               4 :         CheckedInt<T> min_value_approx_sqrt = -max_value_approx_sqrt;
     268                 : 
     269               4 :         VERIFY_IS_VALID(min_value_approx_sqrt);
     270               4 :         VERIFY_IS_VALID(min_value_approx_sqrt * two);
     271               4 :         VERIFY_IS_INVALID(min_value_approx_sqrt * max_value_approx_sqrt);
     272               4 :         VERIFY_IS_INVALID(min_value_approx_sqrt * min_value_approx_sqrt);
     273                 :     }
     274                 : 
     275                 :     // make sure to check all 4 paths in signed multiplication validity check.
     276                 :     // test positive * positive
     277               8 :     VERIFY_IS_VALID(max_value * one);
     278               8 :     VERIFY(max_value * one == max_value);
     279               8 :     VERIFY_IS_INVALID(max_value * two);
     280               8 :     VERIFY_IS_VALID(max_value_over_two * two);
     281               8 :     VERIFY((max_value_over_two + max_value_over_two) == (max_value_over_two * two));
     282                 : 
     283                 :     if (is_signed) {
     284                 :         // test positive * negative
     285               4 :         VERIFY_IS_VALID(max_value * neg_one);
     286               4 :         VERIFY_IS_VALID(-max_value);
     287               4 :         VERIFY(max_value * neg_one == -max_value);
     288               4 :         VERIFY_IS_VALID(one * min_value);
     289               4 :         VERIFY_IS_INVALID(max_value * neg_two);
     290               4 :         VERIFY_IS_VALID(max_value_over_two * neg_two);
     291               4 :         VERIFY_IS_VALID(two * min_value_over_two);
     292               4 :         VERIFY_IS_VALID((max_value_over_two + one) * neg_two);
     293               4 :         VERIFY_IS_INVALID((max_value_over_two + two) * neg_two);
     294               4 :         VERIFY_IS_INVALID(two * (min_value_over_two - one));
     295                 : 
     296                 :         // test negative * positive
     297               4 :         VERIFY_IS_VALID(min_value * one);
     298               4 :         VERIFY_IS_VALID(min_value_plus_one * one);
     299               4 :         VERIFY_IS_INVALID(min_value * two);
     300               4 :         VERIFY_IS_VALID(min_value_over_two * two);
     301               4 :         VERIFY(min_value_over_two * two == min_value);
     302               4 :         VERIFY_IS_INVALID((min_value_over_two - one) * neg_two);
     303               4 :         VERIFY_IS_INVALID(neg_two * max_value);
     304               4 :         VERIFY_IS_VALID(min_value_over_two * two);
     305               4 :         VERIFY(min_value_over_two * two == min_value);
     306               4 :         VERIFY_IS_VALID(neg_two * max_value_over_two);
     307               4 :         VERIFY_IS_INVALID((min_value_over_two - one) * two);
     308               4 :         VERIFY_IS_VALID(neg_two * (max_value_over_two + one));
     309               4 :         VERIFY_IS_INVALID(neg_two * (max_value_over_two + two));
     310                 : 
     311                 :         // test negative * negative
     312               4 :         VERIFY_IS_INVALID(min_value * neg_one);
     313               4 :         VERIFY_IS_VALID(min_value_plus_one * neg_one);
     314               4 :         VERIFY(min_value_plus_one * neg_one == max_value);
     315               4 :         VERIFY_IS_INVALID(min_value * neg_two);
     316               4 :         VERIFY_IS_INVALID(min_value_over_two * neg_two);
     317               4 :         VERIFY_IS_INVALID(neg_one * min_value);
     318               4 :         VERIFY_IS_VALID(neg_one * min_value_plus_one);
     319               4 :         VERIFY(neg_one * min_value_plus_one == max_value);
     320               4 :         VERIFY_IS_INVALID(neg_two * min_value);
     321               4 :         VERIFY_IS_INVALID(neg_two * min_value_over_two);
     322                 :     }
     323                 : 
     324                 :     /* division checks */
     325                 : 
     326               8 :     VERIFY_IS_VALID(one / one);
     327               8 :     VERIFY(one / one == one);
     328               8 :     VERIFY_IS_VALID(three / three);
     329               8 :     VERIFY(three / three == one);
     330               8 :     VERIFY_IS_VALID(four / two);
     331               8 :     VERIFY(four / two == two);
     332               8 :     VERIFY((four*three)/four == three);
     333                 : 
     334                 :     // check that div by zero is invalid
     335               8 :     VERIFY_IS_INVALID(zero / zero);
     336               8 :     VERIFY_IS_INVALID(one / zero);
     337               8 :     VERIFY_IS_INVALID(two / zero);
     338               8 :     VERIFY_IS_INVALID(neg_one / zero);
     339               8 :     VERIFY_IS_INVALID(max_value / zero);
     340               8 :     VERIFY_IS_INVALID(min_value / zero);
     341                 : 
     342                 :     if (is_signed) {
     343                 :         // check that min_value / -1 is invalid
     344               4 :         VERIFY_IS_INVALID(min_value / neg_one);
     345                 : 
     346                 :         // check that the test for div by -1 isn't banning other numerators than min_value
     347               4 :         VERIFY_IS_VALID(one / neg_one);
     348               4 :         VERIFY_IS_VALID(zero / neg_one);
     349               4 :         VERIFY_IS_VALID(neg_one / neg_one);
     350               4 :         VERIFY_IS_VALID(max_value / neg_one);
     351                 :     }
     352                 : 
     353                 :     /* check that invalidity is correctly preserved by arithmetic ops */
     354                 : 
     355               8 :     CheckedInt<T> some_invalid = max_value + max_value;
     356               8 :     VERIFY_IS_INVALID(some_invalid + zero);
     357               8 :     VERIFY_IS_INVALID(some_invalid - zero);
     358               8 :     VERIFY_IS_INVALID(zero + some_invalid);
     359               8 :     VERIFY_IS_INVALID(zero - some_invalid);
     360               8 :     VERIFY_IS_INVALID(-some_invalid);
     361               8 :     VERIFY_IS_INVALID(some_invalid * zero);
     362               8 :     VERIFY_IS_INVALID(some_invalid * one);
     363               8 :     VERIFY_IS_INVALID(zero * some_invalid);
     364               8 :     VERIFY_IS_INVALID(one * some_invalid);
     365               8 :     VERIFY_IS_INVALID(some_invalid / zero);
     366               8 :     VERIFY_IS_INVALID(some_invalid / one);
     367               8 :     VERIFY_IS_INVALID(zero / some_invalid);
     368               8 :     VERIFY_IS_INVALID(one / some_invalid);
     369               8 :     VERIFY_IS_INVALID(some_invalid + some_invalid);
     370               8 :     VERIFY_IS_INVALID(some_invalid - some_invalid);
     371               8 :     VERIFY_IS_INVALID(some_invalid * some_invalid);
     372               8 :     VERIFY_IS_INVALID(some_invalid / some_invalid);
     373                 : 
     374                 :     /* check that mixing checked integers with plain integers in expressions is allowed */
     375                 : 
     376               8 :     VERIFY(one + T(2) == three);
     377               8 :     VERIFY(2 + one == three);
     378                 :     {
     379               8 :         CheckedInt<T> x = one;
     380               8 :         x += 2;
     381               8 :         VERIFY(x == three);
     382                 :     }
     383               8 :     VERIFY(two - 1 == one);
     384               8 :     VERIFY(2 - one == one);
     385                 :     {
     386               8 :         CheckedInt<T> x = two;
     387               8 :         x -= 1;
     388               8 :         VERIFY(x == one);
     389                 :     }
     390               8 :     VERIFY(one * 2 == two);
     391               8 :     VERIFY(2 * one == two);
     392                 :     {
     393               8 :         CheckedInt<T> x = one;
     394               8 :         x *= 2;
     395               8 :         VERIFY(x == two);
     396                 :     }
     397               8 :     VERIFY(four / 2 == two);
     398               8 :     VERIFY(4 / two == two);
     399                 :     {
     400               8 :         CheckedInt<T> x = four;
     401               8 :         x /= 2;
     402               8 :         VERIFY(x == two);
     403                 :     }
     404                 : 
     405               8 :     VERIFY(one == 1);
     406               8 :     VERIFY(1 == one);
     407               8 :     VERIFY_IS_FALSE(two == 1);
     408               8 :     VERIFY_IS_FALSE(1 == two);
     409               8 :     VERIFY_IS_FALSE(some_invalid == 1);
     410               8 :     VERIFY_IS_FALSE(1 == some_invalid);
     411                 : 
     412                 :     /* Check that construction of CheckedInt from an integer value of a mismatched type is checked */
     413                 : 
     414                 :     #define VERIFY_CONSTRUCTION_FROM_INTEGER_TYPE(U) \
     415                 :     { \
     416                 :         bool is_U_signed = integer_traits<U>::is_signed; \
     417                 :         VERIFY_IS_VALID(CheckedInt<T>(U(0))); \
     418                 :         VERIFY_IS_VALID(CheckedInt<T>(U(1))); \
     419                 :         VERIFY_IS_VALID(CheckedInt<T>(U(100))); \
     420                 :         if (is_U_signed) \
     421                 :             VERIFY_IS_VALID_IF(CheckedInt<T>(U(-1)), is_signed); \
     422                 :         if (sizeof(U) > sizeof(T)) \
     423                 :             VERIFY_IS_INVALID(CheckedInt<T>(U(integer_traits<T>::max_value())+1)); \
     424                 :         VERIFY_IS_VALID_IF(CheckedInt<T>(integer_traits<U>::max_value()), \
     425                 :             (sizeof(T) > sizeof(U) || ((sizeof(T) == sizeof(U)) && (is_U_signed || !is_signed)))); \
     426                 :         VERIFY_IS_VALID_IF(CheckedInt<T>(integer_traits<U>::min_value()), \
     427                 :             is_U_signed == false ? 1 : \
     428                 :             bool(is_signed) == false ? 0 : \
     429                 :             sizeof(T) >= sizeof(U)); \
     430                 :     }
     431               8 :     VERIFY_CONSTRUCTION_FROM_INTEGER_TYPE(PRInt8)
     432               8 :     VERIFY_CONSTRUCTION_FROM_INTEGER_TYPE(PRUint8)
     433               8 :     VERIFY_CONSTRUCTION_FROM_INTEGER_TYPE(PRInt16)
     434               8 :     VERIFY_CONSTRUCTION_FROM_INTEGER_TYPE(PRUint16)
     435               8 :     VERIFY_CONSTRUCTION_FROM_INTEGER_TYPE(PRInt32)
     436               8 :     VERIFY_CONSTRUCTION_FROM_INTEGER_TYPE(PRUint32)
     437               8 :     VERIFY_CONSTRUCTION_FROM_INTEGER_TYPE(PRInt64)
     438               8 :     VERIFY_CONSTRUCTION_FROM_INTEGER_TYPE(PRUint64)
     439                 : 
     440                 :     /* Test increment/decrement operators */
     441                 : 
     442               8 :     CheckedInt<T> x, y;
     443               8 :     x = one;
     444               8 :     y = x++;
     445               8 :     VERIFY(x == two);
     446               8 :     VERIFY(y == one);
     447               8 :     x = one;
     448               8 :     y = ++x;
     449               8 :     VERIFY(x == two);
     450               8 :     VERIFY(y == two);
     451               8 :     x = one;
     452               8 :     y = x--;
     453               8 :     VERIFY(x == zero);
     454               8 :     VERIFY(y == one);
     455               8 :     x = one;
     456               8 :     y = --x;
     457               8 :     VERIFY(x == zero);
     458               8 :     VERIFY(y == zero);
     459               8 :     x = max_value;
     460               8 :     VERIFY_IS_VALID(x++);
     461               8 :     x = max_value;
     462               8 :     VERIFY_IS_INVALID(++x);
     463               8 :     x = min_value;
     464               8 :     VERIFY_IS_VALID(x--);
     465               8 :     x = min_value;
     466               8 :     VERIFY_IS_INVALID(--x);
     467                 : }
     468                 : 
     469                 : } // end namespace CheckedInt_test
     470                 : 
     471               1 : int main()
     472                 : {
     473               1 :     CheckedInt_test::test<PRInt8>();
     474               1 :     CheckedInt_test::test<PRUint8>();
     475               1 :     CheckedInt_test::test<PRInt16>();
     476               1 :     CheckedInt_test::test<PRUint16>();
     477               1 :     CheckedInt_test::test<PRInt32>();
     478               1 :     CheckedInt_test::test<PRUint32>();
     479               1 :     CheckedInt_test::test<PRInt64>();
     480               1 :     CheckedInt_test::test<PRUint64>();
     481                 : 
     482               1 :     std::cerr << CheckedInt_test::g_tests_failed << " tests failed, "
     483               2 :               << CheckedInt_test::g_tests_passed << " tests passed out of "
     484               2 :               << CheckedInt_test::g_tests_failed + CheckedInt_test::g_tests_passed
     485               1 :               << " tests." << std::endl;
     486                 : 
     487               1 :     return CheckedInt_test::g_tests_failed > 0;
     488               3 : }

Generated by: LCOV version 1.7