| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122 |
- ///////////////////////////////////////////////////////////////////////////////
- // Copyright (c) Electronic Arts Inc. All rights reserved.
- ///////////////////////////////////////////////////////////////////////////////
- #include <EABase/eabase.h>
- #include <EAStdC/Int128_t.h>
- #include <EAStdCTest/EAStdCTest.h>
- #include <EATest/EATest.h>
- #ifdef _MSC_VER
- #pragma warning(push, 0)
- #endif
- #include <ctype.h>
- #include <math.h>
- #include <string.h>
- #include <stdio.h>
- #ifdef _MSC_VER
- #pragma warning(pop)
- #endif
- static inline double FloatAbsoluteDifference(float x1, float x2)
- {
- return (x1 < x2) ? (x2 - x1) : (x1 - x2);
- }
- static inline bool FloatEqual(float x1, float x2)
- {
- if(x1 < 1e-7f)
- return (x2 < 1e-7f);
- else if(x2 < 1e-7f)
- return (x1 < 1e-7f);
- else
- return FloatAbsoluteDifference((x1 - x2) / x1, 1e-7f) < 1e-5f;
- }
- static bool CompareSelfTestResult(const char* p1, const char* p2)
- {
- return strcmp(p1, p2) == 0;
- }
- int TestInt128()
- {
- using namespace EA::StdC;
- EA::UnitTest::Report("TestInt128\n");
- int nErrorCount(0);
- char array[256];
- bool bResult;
- { // Test of small string assignment
- EA::StdC::int128_t x("10345");
- x.Int128ToStr(array, NULL, 10);
- EATEST_VERIFY(CompareSelfTestResult(array, "10345"));
- }
- { // Test of small string assignment
- EA::StdC::uint128_t x("10345");
- x.Int128ToStr(array, NULL, 10);
- EATEST_VERIFY(CompareSelfTestResult(array, "10345"));
- }
- { // Test of small string assignment
- EA::StdC::int128_t x("-10345");
- x.Int128ToStr(array, NULL, 10);
- EATEST_VERIFY(CompareSelfTestResult(array, "-10345"));
- }
- { // Test of small string assignment
- EA::StdC::int128_t x("0xabcd1234fefe", 16);
- x.Int128ToStr(array, NULL, 16);
- EATEST_VERIFY(CompareSelfTestResult(array, "0x00000000000000000000abcd1234fefe"));
- x.Int128ToStr(array, NULL, 16, EA::StdC::int128_t::kLZEnable, EA::StdC::int128_t::kPrefixEnable);
- EATEST_VERIFY(CompareSelfTestResult(array, "0x00000000000000000000abcd1234fefe"));
- x.Int128ToStr(array, NULL, 16, EA::StdC::int128_t::kLZDisable, EA::StdC::int128_t::kPrefixDisable);
- EATEST_VERIFY_F(CompareSelfTestResult(array, "abcd1234fefe"), "%s %s", array, "abcd1234fefe");
- x.Int128ToStr(array, NULL, 16,EA::StdC::int128_t:: kLZEnable, EA::StdC::int128_t::kPrefixDisable);
- EATEST_VERIFY(CompareSelfTestResult(array, "00000000000000000000abcd1234fefe"));
- x.Int128ToStr(array, NULL, 16, EA::StdC::int128_t::kLZDisable, EA::StdC::int128_t::kPrefixEnable);
- EATEST_VERIFY_F(CompareSelfTestResult(array, "0xabcd1234fefe"), "%s %s", array, "abcd1234fefe");
- }
- { // Test of small string assignment
- EA::StdC::uint128_t x("0xabcd1234fefe", 16);
- x.Int128ToStr(array, NULL, 16);
- EATEST_VERIFY(CompareSelfTestResult(array, "0x00000000000000000000abcd1234fefe"));
- x.Int128ToStr(array, NULL, 16, EA::StdC::uint128_t::kLZEnable, EA::StdC::uint128_t::kPrefixEnable);
- EATEST_VERIFY(CompareSelfTestResult(array, "0x00000000000000000000abcd1234fefe"));
- x.Int128ToStr(array, NULL, 16, EA::StdC::uint128_t::kLZDisable, EA::StdC::uint128_t::kPrefixDisable);
- EATEST_VERIFY_F(CompareSelfTestResult(array, "abcd1234fefe"), "%s %s", array, "abcd1234fefe");
- x.Int128ToStr(array, NULL, 16, EA::StdC::uint128_t::kLZEnable, EA::StdC::uint128_t::kPrefixDisable);
- EATEST_VERIFY(CompareSelfTestResult(array, "00000000000000000000abcd1234fefe"));
- x.Int128ToStr(array, NULL, 16, EA::StdC::uint128_t::kLZDisable, EA::StdC::uint128_t::kPrefixEnable);
- EATEST_VERIFY_F(CompareSelfTestResult(array, "0xabcd1234fefe"), "%s %s", array, "abcd1234fefe");
- }
- { // Test of small string assignment
- EA::StdC::int128_t x("1010101010", 2);
- x.Int128ToStr(array, NULL, 2);
- EATEST_VERIFY(CompareSelfTestResult(array, "1010101010"));
- x.Int128ToStr(array, NULL, 2, EA::StdC::int128_t::kLZEnable, EA::StdC::uint128_t::kPrefixEnable);
- EATEST_VERIFY(CompareSelfTestResult(array, "0b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001010101010"));
- x.Int128ToStr(array, NULL, 2, EA::StdC::int128_t::kLZDisable, EA::StdC::uint128_t::kPrefixDisable);
- EATEST_VERIFY(CompareSelfTestResult(array, "1010101010"));
- x.Int128ToStr(array, NULL, 2, EA::StdC::int128_t::kLZEnable, EA::StdC::uint128_t::kPrefixDisable);
- EATEST_VERIFY(CompareSelfTestResult(array, "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001010101010"));
- x.Int128ToStr(array, NULL, 2, EA::StdC::int128_t::kLZDisable, EA::StdC::uint128_t::kPrefixEnable);
- EATEST_VERIFY(CompareSelfTestResult(array, "0b1010101010"));
- }
- { // Test of small string assignment
- EA::StdC::uint128_t x("1010101010", 2);
- x.Int128ToStr(array, NULL, 2);
- EATEST_VERIFY(CompareSelfTestResult(array, "1010101010"));
- x.Int128ToStr(array, NULL, 2, EA::StdC::uint128_t::kLZEnable, EA::StdC::uint128_t::kPrefixEnable);
- EATEST_VERIFY(CompareSelfTestResult(array, "0b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001010101010"));
- x.Int128ToStr(array, NULL, 2, EA::StdC::uint128_t::kLZDisable, EA::StdC::uint128_t::kPrefixDisable);
- EATEST_VERIFY(CompareSelfTestResult(array, "1010101010"));
- x.Int128ToStr(array, NULL, 2, EA::StdC::uint128_t::kLZEnable, EA::StdC::uint128_t::kPrefixDisable);
- EATEST_VERIFY(CompareSelfTestResult(array, "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001010101010"));
- x.Int128ToStr(array, NULL, 2, EA::StdC::uint128_t::kLZDisable, EA::StdC::uint128_t::kPrefixEnable);
- EATEST_VERIFY(CompareSelfTestResult(array, "0b1010101010"));
- }
- { // Test of large string assignment
- EA::StdC::int128_t x("141183460469231731687303715884105728");
- x.Int128ToStr(array, NULL, 10);
- EATEST_VERIFY(CompareSelfTestResult(array, "141183460469231731687303715884105728"));
- }
- { // Test of large string assignment
- EA::StdC::uint128_t x("141183460469231731687303715884105728");
- x.Int128ToStr(array, NULL, 10);
- EATEST_VERIFY(CompareSelfTestResult(array, "141183460469231731687303715884105728"));
- }
- { // Test of large string assignment
- EA::StdC::int128_t x("0xabcd1234fefeabcd123abcd1234fef", 16);
- x.Int128ToStr(array, NULL, 16);
- EATEST_VERIFY(CompareSelfTestResult(array, "0x00abcd1234fefeabcd123abcd1234fef"));
- }
- { // Test of large string assignment
- EA::StdC::uint128_t x("0xabcd1234fefeabcd123abcd1234fef", 16);
- x.Int128ToStr(array, NULL, 16);
- EATEST_VERIFY(CompareSelfTestResult(array, "0x00abcd1234fefeabcd123abcd1234fef"));
- }
- { // Test of large string assignment
- EA::StdC::int128_t x("1010101010111010111111111000000000001111111110101010101000100111101001010101", 2);
- x.Int128ToStr(array, NULL, 2);
- EATEST_VERIFY(CompareSelfTestResult(array, "1010101010111010111111111000000000001111111110101010101000100111101001010101"));
- }
- { // Test of large string assignment
- EA::StdC::uint128_t x("1010101010111010111111111000000000001111111110101010101000100111101001010101", 2);
- x.Int128ToStr(array, NULL, 2);
- EATEST_VERIFY(CompareSelfTestResult(array, "1010101010111010111111111000000000001111111110101010101000100111101001010101"));
- }
- { //Test of floating point assignment
- EA::StdC::int128_t x(4294967296.f);
- x.Int128ToStr(array, NULL, 10);
- EATEST_VERIFY(CompareSelfTestResult(array, "4294967296"));
- }
- { //Test of floating point assignment
- EA::StdC::int128_t x(-12345672704.f);
- x.Int128ToStr(array, NULL, 10);
- EATEST_VERIFY(CompareSelfTestResult(array, "-12345672704"));
- }
- { //Test of floating point assignment
- EA::StdC::uint128_t x(0.0);
- x.Int128ToStr(array, NULL, 10);
- EATEST_VERIFY(CompareSelfTestResult(array, "0"));
- }
- { //Test of floating point assignment
- EA::StdC::uint128_t x(3456345634563456.0);
- x.Int128ToStr(array, NULL, 10);
- EATEST_VERIFY(CompareSelfTestResult(array, "3456345634563456"));
- }
- { //Test of floating point assignment
- EA::StdC::int128_t x(-123456345634563456.0);
- x.Int128ToStr(array, NULL, 10);
- EATEST_VERIFY(CompareSelfTestResult(array, "-123456345634563456"));
- }
- { // Test of EASTDC_INT128_MIN
- EA::StdC::int128_t x(EASTDC_INT128_MIN);
- x.Int128ToStr(array, NULL, 10);
- EATEST_VERIFY(CompareSelfTestResult(array, "-170141183460469231731687303715884105728"));
- }
- { // Test of EASTDC_INT128_MIN
- EA::StdC::int128_t x(EASTDC_INT128_MIN);
- x.Int128ToStr(array, NULL, 16);
- EATEST_VERIFY(CompareSelfTestResult(array, "0x80000000000000000000000000000000"));
- }
- { // Test of EASTDC_UINT128_MIN
- EA::StdC::uint128_t x(EASTDC_UINT128_MIN);
- x.Int128ToStr(array, NULL, 10);
- EATEST_VERIFY(CompareSelfTestResult(array, "0"));
- }
- { // Test of EASTDC_INT128_MAX
- EA::StdC::int128_t x(EASTDC_INT128_MAX);
- x.Int128ToStr(array, NULL, 10);
- EATEST_VERIFY(CompareSelfTestResult(array, "170141183460469231731687303715884105727"));
- }
- { // Test of EASTDC_INT128_MAX
- EA::StdC::int128_t x(EASTDC_INT128_MAX);
- x.Int128ToStr(array, NULL, 16);
- EATEST_VERIFY(CompareSelfTestResult(array, "0x7fffffffffffffffffffffffffffffff"));
- }
- { // Test of EASTDC_UINT128_MAX
- EA::StdC::uint128_t x(EASTDC_UINT128_MAX);
- x.Int128ToStr(array, NULL, 16);
- EATEST_VERIFY(CompareSelfTestResult(array, "0xffffffffffffffffffffffffffffffff"));
- }
- { // Test of unary operator-
- EA::StdC::int128_t x("123456781234567812345678");
- x = -x;
- x.Int128ToStr(array, NULL, 10);
- EATEST_VERIFY(CompareSelfTestResult(array, "-123456781234567812345678"));
- }
- { // Test of unary operator-
- EA::StdC::int128_t x("-123456781234567812345678");
- x = -x;
- x.Int128ToStr(array, NULL, 10);
- EATEST_VERIFY(CompareSelfTestResult(array, "123456781234567812345678"));
- }
- { // Test of unary operator-
- EA::StdC::int128_t x("1234");
- x = -x;
- x.Int128ToStr(array, NULL, 10);
- EATEST_VERIFY(CompareSelfTestResult(array, "-1234"));
- }
- { // Test of unary operator-
- EA::StdC::uint128_t x("-1234");
- x = -x;
- x.Int128ToStr(array, NULL, 10);
- EATEST_VERIFY(CompareSelfTestResult(array, "1234"));
- }
- { // Test of unary operator+
- EA::StdC::int128_t x("123456781234567812345678");
- x = +x;
- x.Int128ToStr(array, NULL, 10);
- EATEST_VERIFY(CompareSelfTestResult(array, "123456781234567812345678"));
- }
- { // Test of unary operator+
- EA::StdC::uint128_t x("123456781234567812345678");
- x = +x;
- x.Int128ToStr(array, NULL, 10);
- EATEST_VERIFY(CompareSelfTestResult(array, "123456781234567812345678"));
- }
- { // Test of unary operator+
- EA::StdC::int128_t x("-123456781234567812345678");
- x = +x;
- x.Int128ToStr(array, NULL, 10);
- EATEST_VERIFY(CompareSelfTestResult(array, "-123456781234567812345678"));
- }
- { // Test of unary operator>>
- EA::StdC::int128_t x("0x77777777000000008888888800000000", 16);
- x >>= 32;
- x.Int128ToStr(array, NULL, 16);
- EATEST_VERIFY(CompareSelfTestResult(array, "0x00000000777777770000000088888888"));
- }
- { // Test of unary operator>>
- EA::StdC::uint128_t x("0x77777777000000008888888800000000", 16);
- x >>= 32;
- x.Int128ToStr(array, NULL, 16);
- EATEST_VERIFY(CompareSelfTestResult(array, "0x00000000777777770000000088888888"));
- }
- { // Test of unary operator>>
- EA::StdC::int128_t x("0x77777777000000008888888800000000", 16);
- x >>= -16;
- x.Int128ToStr(array, NULL, 16);
- EATEST_VERIFY(CompareSelfTestResult(array, "0x77770000000088888888000000000000"));
- }
- { // Test of unary operator>>
- EA::StdC::uint128_t x("0x77777777000000008888888800000000", 16);
- x >>= -16;
- x.Int128ToStr(array, NULL, 16);
- EATEST_VERIFY(CompareSelfTestResult(array, "0x77770000000088888888000000000000"));
- }
- { // Test of unary operator<<
- EA::StdC::int128_t x("0x77777777000000008888888800000000", 16);
- x <<= 32;
- x.Int128ToStr(array, NULL, 16);
- EATEST_VERIFY(CompareSelfTestResult(array, "0x00000000888888880000000000000000"));
- }
- { // Test of unary operator<<
- EA::StdC::uint128_t x("0x77777777000000008888888800000000", 16);
- x <<= 32;
- x.Int128ToStr(array, NULL, 16);
- EATEST_VERIFY(CompareSelfTestResult(array, "0x00000000888888880000000000000000"));
- }
- { // Test of unary operator>>
- EA::StdC::int128_t x("0x77777777000000008888888800000000", 16);
- x <<= -16;
- x.Int128ToStr(array, NULL, 16);
- EATEST_VERIFY(CompareSelfTestResult(array, "0x00007777777700000000888888880000"));
- }
- { // Test of unary operator>>
- EA::StdC::uint128_t x("0x77777777000000008888888800000000", 16);
- x <<= -16;
- x.Int128ToStr(array, NULL, 16);
- EATEST_VERIFY(CompareSelfTestResult(array, "0x00007777777700000000888888880000"));
- }
- { // Test of unary operator!
- EA::StdC::int128_t x("0");
- bResult = !x;
- EATEST_VERIFY(bResult);
- }
- { // Test of unary operator!
- EA::StdC::uint128_t x("0");
- bResult = !x;
- EATEST_VERIFY(bResult);
- }
- { // Test of unary operator!
- EA::StdC::int128_t x("-1");
- bResult = !x;
- EATEST_VERIFY(!bResult);
- }
- { // Test of unary operator!
- EA::StdC::uint128_t x("-1");
- bResult = !x;
- EATEST_VERIFY(!bResult);
- }
- { // Test of unary operator~
- EA::StdC::int128_t x("0x77777777000000008888888800000000", 16);
- x = ~x;
- x.Int128ToStr(array, NULL, 16);
- EATEST_VERIFY(CompareSelfTestResult(array, "0x88888888ffffffff77777777ffffffff"));
- }
- { // Test of unary operator~
- EA::StdC::uint128_t x("0x77777777000000008888888800000000", 16);
- x = ~x;
- x.Int128ToStr(array, NULL, 16);
- EATEST_VERIFY(CompareSelfTestResult(array, "0x88888888ffffffff77777777ffffffff"));
- }
- { // Test of operator^
- EA::StdC::int128_t x("1010101010101010101010101010101010101010101010101010101010101010", 2);
- EA::StdC::int128_t y("0101010101010101010101010101010101111111111111111111111111111111", 2);
- x = x ^ y;
- x.Int128ToStr(array, NULL, 2);
- EATEST_VERIFY(CompareSelfTestResult(array, "1111111111111111111111111111111111010101010101010101010101010101"));
- }
- { // Test of operator^
- EA::StdC::uint128_t x("1010101010101010101010101010101010101010101010101010101010101010", 2);
- EA::StdC::uint128_t y("0101010101010101010101010101010101111111111111111111111111111111", 2);
- x = x ^ y;
- x.Int128ToStr(array, NULL, 2);
- EATEST_VERIFY(CompareSelfTestResult(array, "1111111111111111111111111111111111010101010101010101010101010101"));
- }
- { // Test of operator|
- EA::StdC::int128_t x("1010101010101010101010101010101010101010101010101010101010101010", 2);
- EA::StdC::int128_t y("0101010101010101010101010101010101111111111111111111111111111111", 2);
- x = x | y;
- x.Int128ToStr(array, NULL, 2);
- EATEST_VERIFY(CompareSelfTestResult(array, "1111111111111111111111111111111111111111111111111111111111111111"));
- }
- { // Test of operator|
- EA::StdC::uint128_t x("1010101010101010101010101010101010101010101010101010101010101010", 2);
- EA::StdC::uint128_t y("0101010101010101010101010101010101111111111111111111111111111111", 2);
- x = x | y;
- x.Int128ToStr(array, NULL, 2);
- EATEST_VERIFY(CompareSelfTestResult(array, "1111111111111111111111111111111111111111111111111111111111111111"));
- }
- { // Test of operator&
- EA::StdC::int128_t x("1010101010101010101010101010101010101010101010101010101010101010", 2);
- EA::StdC::int128_t y("0101010101010101010101010101010101111111111111111111111111111111", 2);
- x = x & y;
- x.Int128ToStr(array, NULL, 2);
- EATEST_VERIFY(CompareSelfTestResult(array, "101010101010101010101010101010"));
- }
- { // Test of operator&
- EA::StdC::uint128_t x("1010101010101010101010101010101010101010101010101010101010101010", 2);
- EA::StdC::uint128_t y("0101010101010101010101010101010101111111111111111111111111111111", 2);
- x = x & y;
- x.Int128ToStr(array, NULL, 2);
- EATEST_VERIFY(CompareSelfTestResult(array, "101010101010101010101010101010"));
- }
- { // Test of operator==
- EA::StdC::int128_t x("123456781234567812345678");
- EA::StdC::int128_t y("123456781234567812345678");
- bResult = (x == y);
- EATEST_VERIFY(bResult);
- }
- { // Test of operator==
- EA::StdC::uint128_t x("123456781234567812345678");
- EA::StdC::uint128_t y("123456781234567812345678");
- bResult = (x == y);
- EATEST_VERIFY(bResult);
- }
- { // Test of operator==
- EA::StdC::int128_t x("123456781234567812345678");
- bResult = (x == 100);
- EATEST_VERIFY(!bResult);
- }
- { // Test of operator==
- EA::StdC::uint128_t x("123456781234567812345678");
- bResult = (x == 100L);
- EATEST_VERIFY(!bResult);
- }
- { // Test of operator!=
- EA::StdC::int128_t x("123123123123123123");
- EA::StdC::int128_t y("123123123123123123");
- bResult = (x != y);
- EATEST_VERIFY(!bResult);
- }
- { // Test of operator!=
- EA::StdC::uint128_t x("123123123123123123");
- EA::StdC::uint128_t y("123123123123123123");
- bResult = (x != y);
- EATEST_VERIFY(!bResult);
- }
- { // Test of operator!=
- EA::StdC::int128_t x("123456781234567812345678");
- bResult = (x != 0x12341234L);
- EATEST_VERIFY(bResult);
- }
- { // Test of operator!=
- EA::StdC::uint128_t x("123456781234567812345678");
- bResult = (x != 0x12341234L);
- EATEST_VERIFY(bResult);
- }
- { // Test of operator>
- EA::StdC::int128_t x("1000");
- EA::StdC::int128_t y("2000");
- bResult = (x > y);
- EATEST_VERIFY(!bResult);
- }
- { // Test of operator>
- EA::StdC::uint128_t x("1000");
- EA::StdC::uint128_t y("2000");
- bResult = (x > y);
- EATEST_VERIFY(!bResult);
- }
- { // Test of operator>
- EA::StdC::int128_t x("9999999999999999999999999999999");
- EA::StdC::int128_t y("8888888888888888888888888888888");
- bResult = (x > y);
- EATEST_VERIFY(bResult);
- }
- { // Test of operator>
- EA::StdC::uint128_t x("9999999999999999999999999999999");
- EA::StdC::uint128_t y("8888888888888888888888888888888");
- bResult = (x > y);
- EATEST_VERIFY(bResult);
- }
- { // Test of operator>
- EA::StdC::int128_t x("-9999999999999999999999999999999");
- EA::StdC::int128_t y("-8888888888888888888888888888888");
- bResult = (x > y);
- EATEST_VERIFY(!bResult);
- }
- { // Test of operator>
- EA::StdC::int128_t x("-1000");
- EA::StdC::int128_t y("-2000");
- bResult = (x > y);
- EATEST_VERIFY(bResult);
- }
- { // Test of operator>
- EA::StdC::int128_t x("1000");
- EA::StdC::int128_t y("-2000");
- bResult = (x > y);
- EATEST_VERIFY(bResult);
- }
- { // Test of operator>
- EA::StdC::int128_t x("-1000");
- EA::StdC::int128_t y("2000");
- bResult = (x > y);
- EATEST_VERIFY(!bResult);
- }
- { // Test of operator>=
- EA::StdC::int128_t x("123456781234567812345678");
- EA::StdC::int128_t y("123456781234567812345678");
- bResult = (x >= y);
- EATEST_VERIFY(bResult);
- }
- { // Test of operator>=
- EA::StdC::uint128_t x("123456781234567812345678");
- EA::StdC::uint128_t y("123456781234567812345678");
- bResult = (x >= y);
- EATEST_VERIFY(bResult);
- }
- { // Test of operator>=
- EA::StdC::int128_t x("-12345678");
- bResult = ((int32_t)-12345678 >= x);
- EATEST_VERIFY(bResult);
- }
- { // Test of operator>=
- EA::StdC::uint128_t x("12345678");
- bResult = ((int32_t)12345679 >= x);
- EATEST_VERIFY(bResult);
- }
- { // Test of operator>=
- EA::StdC::uint128_t x("12345679");
- bResult = ((int32_t)1234567 >= x);
- EATEST_VERIFY(!bResult);
- }
- { // Test of AsBool
- EA::StdC::int128_t x("0");
- bResult = (x.AsBool());
- EATEST_VERIFY(!bResult);
- }
- { // Test of AsBool
- EA::StdC::uint128_t x("0");
- bResult = (x.AsBool());
- EATEST_VERIFY(!bResult);
- }
- { // Test of AsBool
- EA::StdC::int128_t x("-500");
- bResult = (x.AsBool());
- EATEST_VERIFY(bResult);
- }
- { // Test of AsInt8
- EA::StdC::int128_t x("20");
- bResult = (x.AsInt8() == int8_t(20));
- EATEST_VERIFY(bResult);
- }
- { // Test of AsInt8
- EA::StdC::uint128_t x("20");
- bResult = (x.AsInt8() == int8_t(20));
- EATEST_VERIFY(bResult);
- }
- { // Test of AsInt8
- uint64_t x64 = UINT64_C(0x3333333322222222);
- bResult = ((int8_t)x64 == int8_t(0x22));
- EATEST_VERIFY(bResult);
- }
- {
- uint64_t x64 = UINT64_C(0x9999999922222222);
- bResult = ((int8_t)x64 == int8_t(0x22));
- EATEST_VERIFY(bResult);
- }
- {
- EA::StdC::uint128_t x("0x55555555444444443333333322222222", 16);
- bResult = (x.AsInt8() == int8_t(0x22));
- EATEST_VERIFY(bResult);
- }
- { // Test of AsInt8
- EA::StdC::int128_t x("-20");
- bResult = (x.AsInt8() == int8_t(-20));
- EATEST_VERIFY(bResult);
- }
- { // Test of AsInt64
- EA::StdC::int128_t x("18374403898749255808");
- #if !defined(__GNUC__) || (__GNUC__ < 3) || (__GNUC__ >= 4)
- bResult = (x.AsUint64() == UINT64_C(18374403898749255808));
- #else
- bResult = (x.AsUint64() == 0xFEFEFEFE80808080ULL);
- #endif
- EATEST_VERIFY(bResult);
- }
- { // Test of AsInt64
- EA::StdC::uint128_t x("18374403898749255808");
- #if !defined(__GNUC__) || (__GNUC__ < 3) || (__GNUC__ >= 4)
- bResult = (x.AsUint64() == UINT64_C(18374403898749255808));
- #else
- bResult = (x.AsUint64() == 0xFEFEFEFE80808080ULL);
- #endif
- EATEST_VERIFY(bResult);
- }
- { // Test of AsInt64
- EA::StdC::int128_t x("-9223372036854775808");
- bResult = (x.AsInt64() == INT64_MIN);
- EATEST_VERIFY(bResult);
- }
- { // Test of AsInt64
- EA::StdC::uint128_t x("8374403898749255808");
- #if !defined(__GNUC__) || (__GNUC__ < 3) || (__GNUC__ >= 4)
- bResult = (x.AsInt64() == INT64_C(8374403898749255808));
- #else
- bResult = (x.AsInt64() == (int64_t)0x7437DBF9F6988080LL);
- #endif
-
- EATEST_VERIFY(bResult);
- }
- { // Test of AsFloat
- EA::StdC::int128_t x("18374403898749255808");
- volatile float actual = x.AsFloat(); // This prevents the FPU from cheating by using higher internal precision.
- #if !defined(__GNUC__) || (__GNUC__ < 3) || (__GNUC__ >= 4)
- volatile float desired = float(UINT64_C(18374403898749255808));
- #else
- volatile float desired = float(0xFEFEFEFE80808080ULL);
- #endif
- bResult = FloatEqual(actual, desired);
- EATEST_VERIFY(bResult);
- }
- { // Test of AsFloat
- EA::StdC::uint128_t x("18374403898749255808");
- volatile float actual = x.AsFloat(); // This prevents the FPU from cheating by using higher internal precision.
- #if !defined(__GNUC__) || (__GNUC__ < 3) || (__GNUC__ >= 4)
- volatile float desired = float(UINT64_C(18374403898749255808));
- #else
- volatile float desired = float(0xFEFEFEFE80808080ULL);
- #endif
- bResult = FloatEqual(actual, desired);
- EATEST_VERIFY(bResult);
- }
- { // Test of AsFloat
- EA::StdC::int128_t x("-18374403898749255808");
- volatile float actual = x.AsFloat(); // This prevents the FPU from cheating by using higher internal precision.
- #if !defined(__GNUC__) || (__GNUC__ < 3) || (__GNUC__ >= 4)
- volatile float desired = -float(UINT64_C(18374403898749255808));
- #else
- volatile float desired = -float(0xFEFEFEFE80808080ULL);
- #endif
- bResult = FloatEqual(actual, desired);
- EATEST_VERIFY(bResult);
- }
- {
- // Test utility functions
- // int GetBit(int nIndex) const;
- // void SetBit(int nIndex, int value);
- // uint8_t GetPartUint8 (int nIndex) const;
- // uint16_t GetPartUint16(int nIndex) const;
- // uint32_t GetPartUint32(int nIndex) const;
- // uint64_t GetPartUint64(int nIndex) const;
- // void SetPartUint8 (int nIndex, uint8_t value);
- // void SetPartUint16(int nIndex, uint16_t value);
- // void SetPartUint32(int nIndex, uint32_t value);
- // void SetPartUint64(int nIndex, uint64_t value);
- // bool IsZero() const;
- // void SetZero();
- // void TwosComplement();
- // void InverseTwosComplement();
- EA::StdC::int128_t x(0);
- // uint8_t GetPartUint8 (int nIndex) const;
- // void SetPartUint8 (int nIndex, uint8_t value);
- for(uint8_t i8 = 0; (uint8_t)(i8 < 16/sizeof(uint8_t)); i8++)
- x.SetPartUint8((int)i8, i8);
- for(uint8_t i8 = 0; (uint8_t)(i8 < 16/sizeof(uint8_t)); i8++)
- EATEST_VERIFY(x.GetPartUint8((int)i8) == i8);
- // uint16_t GetPartUint16(int nIndex) const;
- // void SetPartUint16(int nIndex, uint16_t value);
- for(uint16_t i16 = 0; i16 < (uint16_t)(16/sizeof(uint16_t)); i16++)
- x.SetPartUint16((int)i16, i16);
- for(uint16_t i16 = 0; i16 < (uint16_t)(16/sizeof(uint16_t)); i16++)
- EATEST_VERIFY(x.GetPartUint16((int)i16) == i16);
- // uint32_t GetPartUint32(int nIndex) const;
- // void SetPartUint32(int nIndex, uint32_t value);
- for(uint32_t i32 = 0; (uint32_t)(i32 < 16/sizeof(uint32_t)); i32++)
- x.SetPartUint32((int)i32, i32);
- for(uint32_t i32 = 0; (uint32_t)(i32 < 16/sizeof(uint32_t)); i32++)
- EATEST_VERIFY(x.GetPartUint32((int)i32) == i32);
- // uint64_t GetPartUint64(int nIndex) const;
- // void SetPartUint64(int nIndex, uint64_t value);
- for(uint64_t i64 = 0; i64 < (uint64_t)(16/sizeof(uint64_t)); i64++)
- x.SetPartUint64((int)i64, i64);
- for(uint64_t i64 = 0; i64 < (uint64_t)(16/sizeof(uint64_t)); i64++)
- EATEST_VERIFY(x.GetPartUint64((int)i64) == i64);
- x = EA::StdC::int128_t("0x11223344556677880123456789ABCDEF", 0);
- // uint8_t GetPartUint8 (int nIndex) const;
- bResult = (x.GetPartUint8(0) == 0xEF);
- EATEST_VERIFY(bResult);
- bResult = (x.GetPartUint8(1) == 0xCD);
- EATEST_VERIFY(bResult);
- bResult = (x.GetPartUint8(2) == 0xaB);
- EATEST_VERIFY(bResult);
- bResult = (x.GetPartUint8(3) == 0x89);
- EATEST_VERIFY(bResult);
- bResult = (x.GetPartUint8(4) == 0x67);
- EATEST_VERIFY(bResult);
- bResult = (x.GetPartUint8(5) == 0x45);
- EATEST_VERIFY(bResult);
- bResult = (x.GetPartUint8(6) == 0x23);
- EATEST_VERIFY(bResult);
- bResult = (x.GetPartUint8(7) == 0x01);
- EATEST_VERIFY(bResult);
- bResult = (x.GetPartUint8(8) == 0x88);
- EATEST_VERIFY(bResult);
- bResult = (x.GetPartUint8(9) == 0x77);
- EATEST_VERIFY(bResult);
- bResult = (x.GetPartUint8(10) == 0x66);
- EATEST_VERIFY(bResult);
- bResult = (x.GetPartUint8(11) == 0x55);
- EATEST_VERIFY(bResult);
- bResult = (x.GetPartUint8(12) == 0x44);
- EATEST_VERIFY(bResult);
- bResult = (x.GetPartUint8(13) == 0x33);
- EATEST_VERIFY(bResult);
- bResult = (x.GetPartUint8(14) == 0x22);
- EATEST_VERIFY(bResult);
- bResult = (x.GetPartUint8(15) == 0x11);
- EATEST_VERIFY(bResult);
- // uint16_t GetPartUint16(int nIndex) const;
- bResult = (x.GetPartUint16(0) == 0xCDEF);
- EATEST_VERIFY(bResult);
- bResult = (x.GetPartUint16(1) == 0x89AB);
- EATEST_VERIFY(bResult);
- bResult = (x.GetPartUint16(2) == 0x4567);
- EATEST_VERIFY(bResult);
- bResult = (x.GetPartUint16(3) == 0x0123);
- EATEST_VERIFY(bResult);
- bResult = (x.GetPartUint16(4) == 0x7788);
- EATEST_VERIFY(bResult);
- bResult = (x.GetPartUint16(5) == 0x5566);
- EATEST_VERIFY(bResult);
- bResult = (x.GetPartUint16(6) == 0x3344);
- EATEST_VERIFY(bResult);
- bResult = (x.GetPartUint16(7) == 0x1122);
- EATEST_VERIFY(bResult);
- // uint32_t GetPartUint32(int nIndex) const;
- bResult = (x.GetPartUint32(0) == 0x89ABCDEF);
- EATEST_VERIFY(bResult);
- bResult = (x.GetPartUint32(1) == 0x01234567);
- EATEST_VERIFY(bResult);
- bResult = (x.GetPartUint32(2) == 0x55667788);
- EATEST_VERIFY(bResult);
- bResult = (x.GetPartUint32(3) == 0x11223344);
- EATEST_VERIFY(bResult);
- // uint64_t GetPartUint64(int nIndex) const;
- bResult = (x.GetPartUint64(0) == UINT64_C(0x0123456789ABCDEF));
- EATEST_VERIFY(bResult);
- bResult = (x.GetPartUint64(1) == UINT64_C(0x1122334455667788));
- EATEST_VERIFY(bResult);
- // bool IsZero() const;
- // void SetZero();
- bResult = x.IsZero();
- EATEST_VERIFY(!bResult);
- x.SetZero();
- bResult = x.IsZero();
- EATEST_VERIFY(bResult);
- bResult = (x.GetPartUint64(0) == 0 && x.GetPartUint64(1) == 0);
- EATEST_VERIFY(bResult);
- // int GetBit(int nIndex) const;
- // void SetBit(int nIndex, int value);
- x = EA::StdC::int128_t("0b10101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010", 0);
- for(int i = 0; i < 128; i++)
- EATEST_VERIFY(x.GetBit(i) == (i % 2));
- for(int i = 0; i < 128; i++)
- x.SetBit(i, x.GetBit(i) ? 0 : 1);
- for(int i = 0; i < 128; i++)
- EATEST_VERIFY(x.GetBit(i) == ((i + 1) % 2));
- // void TwosComplement();
- // void InverseTwosComplement();
- x = EA::StdC::int128_t(1);
- x.TwosComplement();
- EATEST_VERIFY(x == -1);
- x.InverseTwosComplement();
- EATEST_VERIFY(x == 1);
- }
- { // Test of operator += / -=
- EA::StdC::int128_t y("-10000000000000000");
- y += 3000L;
- y -= 3000.0;
- y += UINT64_C(3000);
- y -= 3000.f;
- y.Int128ToStr(array, NULL, 10);
- EATEST_VERIFY(CompareSelfTestResult(array, "-10000000000000000"));
- }
- { // Test of math
- EA::StdC::int128_t y;
- y = 120L * EA::StdC::int128_t("-10000000000000000");
- y.Int128ToStr(array, NULL, 10);
- EATEST_VERIFY(CompareSelfTestResult(array, "-1200000000000000000"));
- }
- { // Test of math
- EA::StdC::int128_t x("-10000000000000000");
- EA::StdC::uint128_t y(120L);
- x = x * y;
- x.Int128ToStr(array, NULL, 10);
- EATEST_VERIFY(CompareSelfTestResult(array, "-1200000000000000000"));
- }
- { // Test of math
- EA::StdC::int128_t x((int64_t)INT64_C(-10000000000000000));
- EA::StdC::int128_t y((int64_t)INT64_C(-20000000000000002));
- y *= x;
- y.Int128ToStr(array, NULL, 10);
- EATEST_VERIFY(CompareSelfTestResult(array, "200000000000000020000000000000000"));
- }
- { // Test of math
- EA::StdC::uint128_t x((int64_t)INT64_C(-10000000000000000));
- EA::StdC::uint128_t y((int64_t)INT64_C(-20000000000000002));
- y *= x;
- y.Int128ToStr(array, NULL, 10);
- EATEST_VERIFY(CompareSelfTestResult(array, "200000000000000020000000000000000"));
- }
- { // Test of math
- EA::StdC::int128_t y;
- y = 0L / EA::StdC::int128_t("-10000000000000000");
- y.Int128ToStr(array, NULL, 10);
- EATEST_VERIFY(CompareSelfTestResult(array, "0"));
- }
- { // Test of math
- EA::StdC::uint128_t y;
- y = 0L / EA::StdC::uint128_t("10000000000000000");
- y.Int128ToStr(array, NULL, 10);
- EATEST_VERIFY(CompareSelfTestResult(array, "0"));
- }
- { // Test of math
- EA::StdC::int128_t y;
- y = EA::StdC::int128_t("-10000000000000000") / 10L;
- y.Int128ToStr(array, NULL, 10);
- EATEST_VERIFY(CompareSelfTestResult(array, "-1000000000000000"));
- }
- { // Test of math
- EA::StdC::uint128_t y;
- y = EA::StdC::int128_t("10000000000000000") / 10L;
- y.Int128ToStr(array, NULL, 10);
- EATEST_VERIFY(CompareSelfTestResult(array, "1000000000000000"));
- }
- { // Test of math
- EA::StdC::int128_t x;
- EA::StdC::int128_t y(1);
- EA::StdC::int128_t z(2);
- x = (y ^ z) + (y & z) + (y | z);
- x.Int128ToStr(array, NULL, 10);
- EATEST_VERIFY(CompareSelfTestResult(array, "6"));
- }
- { // Test of math
- EA::StdC::uint128_t x;
- EA::StdC::uint128_t y(1L);
- EA::StdC::uint128_t z(2L);
- x = (y ^ z) + (y & z) + (y | z);
- x.Int128ToStr(array, NULL, 10);
- EATEST_VERIFY(CompareSelfTestResult(array, "6"));
- }
- { // Test of math
- EA::StdC::int128_t x;
- EA::StdC::int128_t y("0x11111111000100001111111100000001", 16);
- EA::StdC::int128_t z("0x22222222000100002222222200000001", 16);
- x = (y ^ z) + (y & z) + (y | z);
- x.Int128ToStr(array, NULL, 16);
- EATEST_VERIFY(CompareSelfTestResult(array, "0x66666666000200006666666600000002"));
- }
- { // Test of math
- EA::StdC::uint128_t x;
- EA::StdC::uint128_t y("0x11111111000100001111111100000001", 16);
- EA::StdC::uint128_t z("0x22222222000100002222222200000001", 16);
- x = (y ^ z) + (y & z) + (y | z);
- x.Int128ToStr(array, NULL, 16);
- EATEST_VERIFY(CompareSelfTestResult(array, "0x66666666000200006666666600000002"));
- }
- { // Test of math
- int32_t a(17);
- int16_t b(26);
- int32_t c(45);
- int64_t d(77);
- uint16_t e(25);
- EA::StdC::int128_t x(13L);
- EA::StdC::int128_t y;
- y = (((x + (a + b) * 37) / c) * x) % (d + e);
- y.Int128ToStr(array, NULL, 10);
- EATEST_VERIFY(CompareSelfTestResult(array, "47"));
- }
- { // Test of math
- int a(17);
- short b(26);
- EA::StdC::int128_t c(45L);
- int64_t d(77);
- unsigned short e(25);
- EA::StdC::uint128_t x(13L);
- EA::StdC::uint128_t y;
- y = (((x + (a + b) * 37L) / c) * x) % (d + e);
- y.Int128ToStr(array, NULL, 10);
- EATEST_VERIFY(CompareSelfTestResult(array, "47"));
- }
- { // Test of math
- EA::StdC::int128_t x;
- EA::StdC::int128_t y("0x11111111000100001111111100000001", 16);
- EA::StdC::uint128_t z("0x22222222000100002222222200000001", 16);
- x = (y ^ z) + (y & z) + (y | z);
- x.Int128ToStr(array, NULL, 16);
- EATEST_VERIFY(CompareSelfTestResult(array, "0x66666666000200006666666600000002"));
- }
- {
- // int128_t int128_t::StrToInt128(const char* pValue, char** ppEnd, int base) const;
- // int128_t int128_t::StrToInt128(const wchar_t* pValue, wchar_t** ppEnd, int base) const;
- // EA::StdC::uint128_t EA::StdC::uint128_t::StrToInt128(const char* pValue, char** ppEnd, int base) const;
- // EA::StdC::uint128_t EA::StdC::uint128_t::StrToInt128(const wchar_t* pValue, wchar_t** ppEnd, int base) const;
- char* pEnd;
- EA::StdC::int128_t i128;
- EA::StdC::uint128_t u128;
- { // Base 2
- char strBase2[] = "0b101_"; // Decimal 5
- i128 = EA::StdC::int128_t::StrToInt128(strBase2, &pEnd, 0);
- EATEST_VERIFY((i128.AsInt32() == 5) && (*pEnd == '_'));
- i128 = EA::StdC::int128_t::StrToInt128(strBase2, &pEnd, 2);
- EATEST_VERIFY((i128.AsInt32() == 5) && (*pEnd == '_'));
- i128 = EA::StdC::int128_t::StrToInt128(strBase2 + 2, &pEnd, 2);
- EATEST_VERIFY((i128.AsInt32() == 5) && (*pEnd == '_'));
- u128 = EA::StdC::uint128_t::StrToInt128(strBase2, &pEnd, 0);
- EATEST_VERIFY((u128.AsInt32() == 5) && (*pEnd == '_'));
- u128 = EA::StdC::uint128_t::StrToInt128(strBase2, &pEnd, 2);
- EATEST_VERIFY((u128.AsInt32() == 5) && (*pEnd == '_'));
- u128 = EA::StdC::uint128_t::StrToInt128(strBase2 + 2, &pEnd, 2);
- EATEST_VERIFY((u128.AsInt32() == 5) && (*pEnd == '_'));
- }
- /* Base 8 is not yet supported by StrToInt128.
- { // Base 8
- char strBase8[] = "032_"; // Decimal 26
- i128 = int128_t::StrToInt128(strBase8, &pEnd, 0);
- EATEST_VERIFY((i128.AsInt32() == 26) && (*pEnd == '_'));
- i128 = int128_t::StrToInt128(strBase8, &pEnd, 8);
- EATEST_VERIFY((i128.AsInt32() == 26) && (*pEnd == '_'));
- u128 = EA::StdC::uint128_t::StrToInt128(strBase8, &pEnd, 0);
- EATEST_VERIFY((u128.AsInt32() == 26) && (*pEnd == '_'));
- u128 = EA::StdC::uint128_t::StrToInt128(strBase8, &pEnd, 8);
- EATEST_VERIFY((u128.AsInt32() == 26) && (*pEnd == '_'));
- }
- */
- { // Base 10
- char strBase10[] = "32_"; // Decimal 32
- i128 = EA::StdC::int128_t::StrToInt128(strBase10, &pEnd, 0);
- EATEST_VERIFY((i128.AsInt32() == 32) && (*pEnd == '_'));
- i128 = EA::StdC::int128_t::StrToInt128(strBase10, &pEnd, 10);
- EATEST_VERIFY((i128.AsInt32() == 32) && (*pEnd == '_'));
- u128 = EA::StdC::uint128_t::StrToInt128(strBase10, &pEnd, 0);
- EATEST_VERIFY((u128.AsInt32() == 32) && (*pEnd == '_'));
- u128 = EA::StdC::uint128_t::StrToInt128(strBase10, &pEnd, 10);
- EATEST_VERIFY((u128.AsInt32() == 32) && (*pEnd == '_'));
- }
- { // Base 16
- char strBase16[] = "0x32_"; // Decimal 50
- i128 = EA::StdC::int128_t::StrToInt128(strBase16, &pEnd, 0);
- EATEST_VERIFY((i128.AsInt32() == 50) && (*pEnd == '_'));
- i128 = EA::StdC::int128_t::StrToInt128(strBase16, &pEnd, 16);
- EATEST_VERIFY((i128.AsInt32() == 50) && (*pEnd == '_'));
- i128 = EA::StdC::int128_t::StrToInt128(strBase16 + 2, &pEnd, 16);
- EATEST_VERIFY((i128.AsInt32() == 50) && (*pEnd == '_'));
- u128 = EA::StdC::uint128_t::StrToInt128(strBase16, &pEnd, 0);
- EATEST_VERIFY((u128.AsInt32() == 50) && (*pEnd == '_'));
- u128 = EA::StdC::uint128_t::StrToInt128(strBase16, &pEnd, 16);
- EATEST_VERIFY((u128.AsInt32() == 50) && (*pEnd == '_'));
- u128 = EA::StdC::uint128_t::StrToInt128(strBase16 + 2, &pEnd, 16);
- EATEST_VERIFY((u128.AsInt32() == 50) && (*pEnd == '_'));
- }
- }
- return nErrorCount;
- }
|