LCOV - code coverage report
Current view: top level - tests - test_as2js_int64.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 197 209 94.3 %
Date: 2014-11-22 Functions: 3 3 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* test_as2js_int64.cpp -- written by Alexis WILKE for Made to Order Software Corp. (c) 2005-2014 */
       2             : 
       3             : /*
       4             : 
       5             : Copyright (c) 2005-2014 Made to Order Software Corp.
       6             : 
       7             : http://snapwebsites.org/project/as2js
       8             : 
       9             : Permission is hereby granted, free of charge, to any
      10             : person obtaining a copy of this software and
      11             : associated documentation files (the "Software"), to
      12             : deal in the Software without restriction, including
      13             : without limitation the rights to use, copy, modify,
      14             : merge, publish, distribute, sublicense, and/or sell
      15             : copies of the Software, and to permit persons to whom
      16             : the Software is furnished to do so, subject to the
      17             : following conditions:
      18             : 
      19             : The above copyright notice and this permission notice
      20             : shall be included in all copies or substantial
      21             : portions of the Software.
      22             : 
      23             : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
      24             : ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
      25             : LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
      26             : FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
      27             : EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
      28             : LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
      29             : WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
      30             : ARISING FROM, OUT OF OR IN CONNECTION WITH THE
      31             : SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
      32             : SOFTWARE.
      33             : 
      34             : */
      35             : 
      36             : #include    "test_as2js_int64.h"
      37             : #include    "test_as2js_main.h"
      38             : 
      39             : #include    "as2js/int64.h"
      40             : #include    "as2js/exceptions.h"
      41             : 
      42             : #include    <cstring>
      43             : #include    <algorithm>
      44             : 
      45             : #include    <cppunit/config/SourcePrefix.h>
      46           4 : CPPUNIT_TEST_SUITE_REGISTRATION( As2JsInt64UnitTests );
      47             : 
      48             : 
      49             : 
      50             : 
      51             : 
      52           1 : void As2JsInt64UnitTests::test_int64()
      53             : {
      54             :     // default constructor gives us zero
      55             :     {
      56           1 :         as2js::Int64   zero;
      57           1 :         CPPUNIT_ASSERT(zero.get() == 0);
      58             :     }
      59             : 
      60             :     // int8_t constructor, copy constructor, copy assignment
      61        1001 :     for(int i(0); i < 1000; ++i)
      62             :     {
      63             :         // generate a random 16 bit number
      64        1000 :         int8_t r(rand());
      65             : 
      66             :         // sign extends properly?
      67        1000 :         as2js::Int64 random(r);
      68        1000 :         CPPUNIT_ASSERT(random.get() == r);
      69             : 
      70             :         // copy works as expected?
      71        1000 :         as2js::Int64 copy(random);
      72        1000 :         CPPUNIT_ASSERT(copy.get() == r);
      73             : 
      74             :         int8_t q;
      75        1000 :         q = static_cast<int8_t>(rand());
      76             : 
      77        1000 :         random = q;
      78        1000 :         CPPUNIT_ASSERT(random.get() == q);
      79             : 
      80        1000 :         CPPUNIT_ASSERT(as2js::compare_utils::is_ordered(random.compare(copy)));
      81        1000 :         CPPUNIT_ASSERT(as2js::compare_utils::is_ordered(copy.compare(random)));
      82        1000 :         if(q < r)
      83             :         {
      84         512 :             CPPUNIT_ASSERT(random.compare(copy) == as2js::compare_t::COMPARE_LESS);
      85         512 :             CPPUNIT_ASSERT(copy.compare(random) == as2js::compare_t::COMPARE_GREATER);
      86             :         }
      87         488 :         else if(q > r)
      88             :         {
      89         484 :             CPPUNIT_ASSERT(random.compare(copy) == as2js::compare_t::COMPARE_GREATER);
      90         484 :             CPPUNIT_ASSERT(copy.compare(random) == as2js::compare_t::COMPARE_LESS);
      91             :         }
      92             :         else
      93             :         {
      94           4 :             CPPUNIT_ASSERT(random.compare(copy) == as2js::compare_t::COMPARE_EQUAL);
      95           4 :             CPPUNIT_ASSERT(copy.compare(random) == as2js::compare_t::COMPARE_EQUAL);
      96             :         }
      97             : 
      98             :         int8_t p;
      99        1000 :         p = static_cast<int8_t>(rand());
     100             : 
     101        1000 :         random.set(p);
     102        1000 :         CPPUNIT_ASSERT(random.get() == p);
     103             :     }
     104             : 
     105             :     // uint8_t constructor, copy constructor, copy assignment
     106        1001 :     for(int i(0); i < 1000; ++i)
     107             :     {
     108             :         // generate a random 16 bit number
     109        1000 :         uint8_t r(rand());
     110             : 
     111             :         // sign extends properly?
     112        1000 :         as2js::Int64 random(r);
     113        1000 :         CPPUNIT_ASSERT(random.get() == r);
     114             : 
     115             :         // copy works as expected?
     116        1000 :         as2js::Int64 copy(random);
     117        1000 :         CPPUNIT_ASSERT(copy.get() == r);
     118             : 
     119             :         uint8_t q;
     120        1000 :         q = static_cast<uint8_t>(rand());
     121             : 
     122        1000 :         random = q;
     123        1000 :         CPPUNIT_ASSERT(random.get() == q);
     124             : 
     125        1000 :         CPPUNIT_ASSERT(as2js::compare_utils::is_ordered(random.compare(copy)));
     126        1000 :         CPPUNIT_ASSERT(as2js::compare_utils::is_ordered(copy.compare(random)));
     127        1000 :         if(q < r)
     128             :         {
     129         499 :             CPPUNIT_ASSERT(random.compare(copy) == as2js::compare_t::COMPARE_LESS);
     130         499 :             CPPUNIT_ASSERT(copy.compare(random) == as2js::compare_t::COMPARE_GREATER);
     131             :         }
     132         501 :         else if(q > r)
     133             :         {
     134         498 :             CPPUNIT_ASSERT(random.compare(copy) == as2js::compare_t::COMPARE_GREATER);
     135         498 :             CPPUNIT_ASSERT(copy.compare(random) == as2js::compare_t::COMPARE_LESS);
     136             :         }
     137             :         else
     138             :         {
     139           3 :             CPPUNIT_ASSERT(random.compare(copy) == as2js::compare_t::COMPARE_EQUAL);
     140           3 :             CPPUNIT_ASSERT(copy.compare(random) == as2js::compare_t::COMPARE_EQUAL);
     141             :         }
     142             : 
     143             :         uint8_t p;
     144        1000 :         p = static_cast<uint8_t>(rand());
     145             : 
     146        1000 :         random.set(p);
     147        1000 :         CPPUNIT_ASSERT(random.get() == p);
     148             :     }
     149             : 
     150             :     // int16_t constructor, copy constructor, copy assignment
     151        1001 :     for(int i(0); i < 1000; ++i)
     152             :     {
     153             :         // generate a random 16 bit number
     154        1000 :         int16_t r(rand());
     155             : 
     156             :         // sign extends properly?
     157        1000 :         as2js::Int64 random(r);
     158        1000 :         CPPUNIT_ASSERT(random.get() == r);
     159             : 
     160             :         // copy works as expected?
     161        1000 :         as2js::Int64 copy(random);
     162        1000 :         CPPUNIT_ASSERT(copy.get() == r);
     163             : 
     164             :         int16_t q;
     165        1000 :         q = (static_cast<int16_t>(rand()) << 16)
     166        1000 :           ^ (static_cast<int16_t>(rand()) <<  0);
     167             : 
     168        1000 :         random = q;
     169        1000 :         CPPUNIT_ASSERT(random.get() == q);
     170             : 
     171        1000 :         CPPUNIT_ASSERT(as2js::compare_utils::is_ordered(random.compare(copy)));
     172        1000 :         CPPUNIT_ASSERT(as2js::compare_utils::is_ordered(copy.compare(random)));
     173        1000 :         if(q < r)
     174             :         {
     175         485 :             CPPUNIT_ASSERT(random.compare(copy) == as2js::compare_t::COMPARE_LESS);
     176         485 :             CPPUNIT_ASSERT(copy.compare(random) == as2js::compare_t::COMPARE_GREATER);
     177             :         }
     178         515 :         else if(q > r)
     179             :         {
     180         515 :             CPPUNIT_ASSERT(random.compare(copy) == as2js::compare_t::COMPARE_GREATER);
     181         515 :             CPPUNIT_ASSERT(copy.compare(random) == as2js::compare_t::COMPARE_LESS);
     182             :         }
     183             :         else
     184             :         {
     185           0 :             CPPUNIT_ASSERT(random.compare(copy) == as2js::compare_t::COMPARE_EQUAL);
     186           0 :             CPPUNIT_ASSERT(copy.compare(random) == as2js::compare_t::COMPARE_EQUAL);
     187             :         }
     188             : 
     189             :         int16_t p;
     190        1000 :         p = (static_cast<int16_t>(rand()) << 16)
     191        1000 :           ^ (static_cast<int16_t>(rand()) <<  0);
     192             : 
     193        1000 :         random.set(p);
     194        1000 :         CPPUNIT_ASSERT(random.get() == p);
     195             :     }
     196             : 
     197             :     // uint16_t constructor, copy constructor, copy assignment
     198        1001 :     for(int i(0); i < 1000; ++i)
     199             :     {
     200             :         // generate a random 16 bit number
     201        1000 :         uint16_t r(rand());
     202             : 
     203             :         // sign extends properly?
     204        1000 :         as2js::Int64 random(r);
     205        1000 :         CPPUNIT_ASSERT(random.get() == r);
     206             : 
     207             :         // copy works as expected?
     208        1000 :         as2js::Int64 copy(random);
     209        1000 :         CPPUNIT_ASSERT(copy.get() == r);
     210             : 
     211             :         uint16_t q;
     212        1000 :         q = (static_cast<uint16_t>(rand()) << 16)
     213        1000 :           ^ (static_cast<uint16_t>(rand()) <<  0);
     214             : 
     215        1000 :         random = q;
     216        1000 :         CPPUNIT_ASSERT(random.get() == q);
     217             : 
     218        1000 :         CPPUNIT_ASSERT(as2js::compare_utils::is_ordered(random.compare(copy)));
     219        1000 :         CPPUNIT_ASSERT(as2js::compare_utils::is_ordered(copy.compare(random)));
     220        1000 :         if(q < r)
     221             :         {
     222         503 :             CPPUNIT_ASSERT(random.compare(copy) == as2js::compare_t::COMPARE_LESS);
     223         503 :             CPPUNIT_ASSERT(copy.compare(random) == as2js::compare_t::COMPARE_GREATER);
     224             :         }
     225         497 :         else if(q > r)
     226             :         {
     227         497 :             CPPUNIT_ASSERT(random.compare(copy) == as2js::compare_t::COMPARE_GREATER);
     228         497 :             CPPUNIT_ASSERT(copy.compare(random) == as2js::compare_t::COMPARE_LESS);
     229             :         }
     230             :         else
     231             :         {
     232           0 :             CPPUNIT_ASSERT(random.compare(copy) == as2js::compare_t::COMPARE_EQUAL);
     233           0 :             CPPUNIT_ASSERT(copy.compare(random) == as2js::compare_t::COMPARE_EQUAL);
     234             :         }
     235             : 
     236             :         uint16_t p;
     237        1000 :         p = (static_cast<uint16_t>(rand()) << 16)
     238        1000 :           ^ (static_cast<uint16_t>(rand()) <<  0);
     239             : 
     240        1000 :         random.set(p);
     241        1000 :         CPPUNIT_ASSERT(random.get() == p);
     242             :     }
     243             : 
     244             :     // int32_t constructor, copy constructor, copy assignment
     245        1001 :     for(int i(0); i < 1000; ++i)
     246             :     {
     247             :         // generate a random 32 bit number
     248             :         int32_t r;
     249        1000 :         r = (static_cast<int32_t>(rand()) << 16)
     250        1000 :           ^ (static_cast<int32_t>(rand()) <<  0);
     251             : 
     252             :         // sign extends properly?
     253        1000 :         as2js::Int64 random(r);
     254        1000 :         CPPUNIT_ASSERT(random.get() == r);
     255             : 
     256             :         // copy works as expected?
     257        1000 :         as2js::Int64 copy(random);
     258        1000 :         CPPUNIT_ASSERT(copy.get() == r);
     259             : 
     260             :         int32_t q;
     261        1000 :         q = (static_cast<int32_t>(rand()) << 16)
     262        1000 :           ^ (static_cast<int32_t>(rand()) <<  0);
     263             : 
     264        1000 :         random = q;
     265        1000 :         CPPUNIT_ASSERT(random.get() == q);
     266             : 
     267        1000 :         CPPUNIT_ASSERT(as2js::compare_utils::is_ordered(random.compare(copy)));
     268        1000 :         CPPUNIT_ASSERT(as2js::compare_utils::is_ordered(copy.compare(random)));
     269        1000 :         if(q < r)
     270             :         {
     271         494 :             CPPUNIT_ASSERT(random.compare(copy) == as2js::compare_t::COMPARE_LESS);
     272         494 :             CPPUNIT_ASSERT(copy.compare(random) == as2js::compare_t::COMPARE_GREATER);
     273             :         }
     274         506 :         else if(q > r)
     275             :         {
     276         506 :             CPPUNIT_ASSERT(random.compare(copy) == as2js::compare_t::COMPARE_GREATER);
     277         506 :             CPPUNIT_ASSERT(copy.compare(random) == as2js::compare_t::COMPARE_LESS);
     278             :         }
     279             :         else
     280             :         {
     281           0 :             CPPUNIT_ASSERT(random.compare(copy) == as2js::compare_t::COMPARE_EQUAL);
     282           0 :             CPPUNIT_ASSERT(copy.compare(random) == as2js::compare_t::COMPARE_EQUAL);
     283             :         }
     284             : 
     285             :         int32_t p;
     286        1000 :         p = (static_cast<int32_t>(rand()) << 16)
     287        1000 :           ^ (static_cast<int32_t>(rand()) <<  0);
     288             : 
     289        1000 :         random.set(p);
     290        1000 :         CPPUNIT_ASSERT(random.get() == p);
     291             :     }
     292             : 
     293             :     // uint32_t constructor, copy constructor, copy assignment
     294        1001 :     for(int i(0); i < 1000; ++i)
     295             :     {
     296             :         // generate a random 32 bit number
     297             :         uint32_t r;
     298        1000 :         r = (static_cast<uint32_t>(rand()) << 16)
     299        1000 :           ^ (static_cast<uint32_t>(rand()) <<  0);
     300             : 
     301             :         // sign extends properly?
     302        1000 :         as2js::Int64 random(r);
     303        1000 :         CPPUNIT_ASSERT(random.get() == r);
     304             : 
     305             :         // copy works as expected?
     306        1000 :         as2js::Int64 copy(random);
     307        1000 :         CPPUNIT_ASSERT(copy.get() == r);
     308             : 
     309             :         uint32_t q;
     310        1000 :         q = (static_cast<uint32_t>(rand()) << 16)
     311        1000 :           ^ (static_cast<uint32_t>(rand()) <<  0);
     312             : 
     313        1000 :         random = q;
     314        1000 :         CPPUNIT_ASSERT(random.get() == q);
     315             : 
     316        1000 :         CPPUNIT_ASSERT(as2js::compare_utils::is_ordered(random.compare(copy)));
     317        1000 :         CPPUNIT_ASSERT(as2js::compare_utils::is_ordered(copy.compare(random)));
     318        1000 :         if(q < r)
     319             :         {
     320         496 :             CPPUNIT_ASSERT(random.compare(copy) == as2js::compare_t::COMPARE_LESS);
     321         496 :             CPPUNIT_ASSERT(copy.compare(random) == as2js::compare_t::COMPARE_GREATER);
     322             :         }
     323         504 :         else if(q > r)
     324             :         {
     325         504 :             CPPUNIT_ASSERT(random.compare(copy) == as2js::compare_t::COMPARE_GREATER);
     326         504 :             CPPUNIT_ASSERT(copy.compare(random) == as2js::compare_t::COMPARE_LESS);
     327             :         }
     328             :         else
     329             :         {
     330           0 :             CPPUNIT_ASSERT(random.compare(copy) == as2js::compare_t::COMPARE_EQUAL);
     331           0 :             CPPUNIT_ASSERT(copy.compare(random) == as2js::compare_t::COMPARE_EQUAL);
     332             :         }
     333             : 
     334             :         uint32_t p;
     335        1000 :         p = (static_cast<uint32_t>(rand()) << 16)
     336        1000 :           ^ (static_cast<uint32_t>(rand()) <<  0);
     337             : 
     338        1000 :         random.set(p);
     339        1000 :         CPPUNIT_ASSERT(random.get() == p);
     340             :     }
     341             : 
     342             :     // int64_t constructor, copy constructor, copy assignment
     343        1001 :     for(int i(0); i < 1000; ++i)
     344             :     {
     345             :         // generate a random 64 bit number
     346             :         int64_t r;
     347        1000 :         r = (static_cast<int64_t>(rand()) << 48)
     348        1000 :           ^ (static_cast<int64_t>(rand()) << 32)
     349        1000 :           ^ (static_cast<int64_t>(rand()) << 16)
     350        1000 :           ^ (static_cast<int64_t>(rand()) <<  0);
     351        1000 :         as2js::Int64 random(r);
     352        1000 :         CPPUNIT_ASSERT(random.get() == r);
     353             : 
     354        1000 :         as2js::Int64 copy(random);
     355        1000 :         CPPUNIT_ASSERT(copy.get() == r);
     356             : 
     357             :         int64_t q;
     358        1000 :         q = (static_cast<int64_t>(rand()) << 48)
     359        1000 :           ^ (static_cast<int64_t>(rand()) << 32)
     360        1000 :           ^ (static_cast<int64_t>(rand()) << 16)
     361        1000 :           ^ (static_cast<int64_t>(rand()) <<  0);
     362             : 
     363        1000 :         random = q;
     364        1000 :         CPPUNIT_ASSERT(random.get() == q);
     365             : 
     366        1000 :         CPPUNIT_ASSERT(as2js::compare_utils::is_ordered(random.compare(copy)));
     367        1000 :         CPPUNIT_ASSERT(as2js::compare_utils::is_ordered(copy.compare(random)));
     368        1000 :         if(q < r)
     369             :         {
     370         489 :             CPPUNIT_ASSERT(random.compare(copy) == as2js::compare_t::COMPARE_LESS);
     371         489 :             CPPUNIT_ASSERT(copy.compare(random) == as2js::compare_t::COMPARE_GREATER);
     372             :         }
     373         511 :         else if(q > r)
     374             :         {
     375         511 :             CPPUNIT_ASSERT(random.compare(copy) == as2js::compare_t::COMPARE_GREATER);
     376         511 :             CPPUNIT_ASSERT(copy.compare(random) == as2js::compare_t::COMPARE_LESS);
     377             :         }
     378             :         else
     379             :         {
     380           0 :             CPPUNIT_ASSERT(random.compare(copy) == as2js::compare_t::COMPARE_EQUAL);
     381           0 :             CPPUNIT_ASSERT(copy.compare(random) == as2js::compare_t::COMPARE_EQUAL);
     382             :         }
     383             : 
     384             :         int64_t p;
     385        1000 :         p = (static_cast<int64_t>(rand()) << 48)
     386        1000 :           ^ (static_cast<int64_t>(rand()) << 32)
     387        1000 :           ^ (static_cast<int64_t>(rand()) << 16)
     388        1000 :           ^ (static_cast<int64_t>(rand()) <<  0);
     389             : 
     390        1000 :         random.set(p);
     391        1000 :         CPPUNIT_ASSERT(random.get() == p);
     392             :     }
     393             : 
     394             :     // uint64_t constructor, copy constructor, copy assignment
     395        1001 :     for(int i(0); i < 1000; ++i)
     396             :     {
     397             :         // generate a random 64 bit number
     398             :         uint64_t r;
     399        1000 :         r = (static_cast<uint64_t>(rand()) << 48)
     400        1000 :           ^ (static_cast<uint64_t>(rand()) << 32)
     401        1000 :           ^ (static_cast<uint64_t>(rand()) << 16)
     402        1000 :           ^ (static_cast<uint64_t>(rand()) <<  0);
     403        1000 :         as2js::Int64 random(r);
     404        1000 :         CPPUNIT_ASSERT(random.get() == static_cast<int64_t>(r));
     405             : 
     406        1000 :         as2js::Int64 copy(random);
     407        1000 :         CPPUNIT_ASSERT(copy.get() == static_cast<int64_t>(r));
     408             : 
     409             :         uint64_t q;
     410        1000 :         q = (static_cast<uint64_t>(rand()) << 48)
     411        1000 :           ^ (static_cast<uint64_t>(rand()) << 32)
     412        1000 :           ^ (static_cast<uint64_t>(rand()) << 16)
     413        1000 :           ^ (static_cast<uint64_t>(rand()) <<  0);
     414             : 
     415        1000 :         random = q;
     416        1000 :         CPPUNIT_ASSERT(random.get() == static_cast<int64_t>(q));
     417             : 
     418             :         // Here the compare expects the signed int64_t...
     419        1000 :         CPPUNIT_ASSERT(as2js::compare_utils::is_ordered(random.compare(copy)));
     420        1000 :         CPPUNIT_ASSERT(as2js::compare_utils::is_ordered(copy.compare(random)));
     421        1000 :         if(static_cast<int64_t>(q) < static_cast<int64_t>(r))
     422             :         {
     423         499 :             CPPUNIT_ASSERT(random.compare(copy) == as2js::compare_t::COMPARE_LESS);
     424         499 :             CPPUNIT_ASSERT(copy.compare(random) == as2js::compare_t::COMPARE_GREATER);
     425             :         }
     426         501 :         else if(static_cast<int64_t>(q) > static_cast<int64_t>(r))
     427             :         {
     428         501 :             CPPUNIT_ASSERT(random.compare(copy) == as2js::compare_t::COMPARE_GREATER);
     429         501 :             CPPUNIT_ASSERT(copy.compare(random) == as2js::compare_t::COMPARE_LESS);
     430             :         }
     431             :         else
     432             :         {
     433           0 :             CPPUNIT_ASSERT(random.compare(copy) == as2js::compare_t::COMPARE_EQUAL);
     434           0 :             CPPUNIT_ASSERT(copy.compare(random) == as2js::compare_t::COMPARE_EQUAL);
     435             :         }
     436             : 
     437             :         uint64_t p;
     438        1000 :         p = (static_cast<uint64_t>(rand()) << 48)
     439        1000 :           ^ (static_cast<uint64_t>(rand()) << 32)
     440        1000 :           ^ (static_cast<uint64_t>(rand()) << 16)
     441        1000 :           ^ (static_cast<uint64_t>(rand()) <<  0);
     442             : 
     443        1000 :         random.set(p);
     444        1000 :         CPPUNIT_ASSERT(random.get() == static_cast<int64_t>(p));
     445             :     }
     446          13 : }
     447             : 
     448             : 
     449             : 
     450             : 
     451             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.10