?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download

  1. #include "UnitTest++/UnitTestPP.h"
  2.  
  3. #include "UnitTest++/MemoryOutStream.h"
  4. #include <cstring>
  5. #include <cstdlib>
  6. #include <climits>
  7. #include <cfloat>
  8.  
  9. using namespace UnitTest;
  10. using namespace std;
  11.  
  12. namespace {
  13.  
  14.    const char* maxSignedIntegralStr(size_t nBytes)
  15.    {
  16.       switch(nBytes)
  17.       {
  18.       case 8:
  19.          return "9223372036854775807";
  20.       case 4:
  21.          return "2147483647";
  22.       case 2:
  23.          return "32767";
  24.       case 1:
  25.          return "127";
  26.       default:
  27.          return "Unsupported signed integral size";
  28.       }
  29.    }
  30.  
  31.    const char* minSignedIntegralStr(size_t nBytes)
  32.    {
  33.       switch(nBytes)
  34.       {
  35.       case 8:
  36.          return "-9223372036854775808";
  37.       case 4:
  38.          return "-2147483648";
  39.       case 2:
  40.          return "-32768";
  41.       case 1:
  42.          return "-128";
  43.       default:
  44.          return "Unsupported signed integral size";
  45.       }
  46.    }
  47.  
  48.    const char* maxUnsignedIntegralStr(size_t nBytes)
  49.    {
  50.       switch(nBytes)
  51.       {
  52.       case 8:
  53.          return "18446744073709551615";
  54.       case 4:
  55.          return "4294967295";
  56.       case 2:
  57.          return "65535";
  58.       case 1:
  59.          return "255";
  60.       default:
  61.          return "Unsupported signed integral size";
  62.       }
  63.    }
  64.  
  65.    TEST(DefaultIsEmptyString)
  66.    {
  67.       MemoryOutStream const stream;
  68.       CHECK(stream.GetText() != 0);
  69.       CHECK_EQUAL("", stream.GetText());
  70.    }
  71.  
  72.    TEST(StreamingTextCopiesCharacters)
  73.    {
  74.       MemoryOutStream stream;
  75.       stream << "Lalala";
  76.       CHECK_EQUAL("Lalala", stream.GetText());
  77.    }
  78.  
  79.    TEST(StreamingMultipleTimesConcatenatesResult)
  80.    {
  81.       MemoryOutStream stream;
  82.       stream << "Bork" << "Foo" << "Bar";
  83.       CHECK_EQUAL("BorkFooBar", stream.GetText());
  84.    }
  85.  
  86.    TEST(StreamingIntWritesCorrectCharacters)
  87.    {
  88.       MemoryOutStream stream;
  89.       stream << (int)123;
  90.       CHECK_EQUAL("123", stream.GetText());
  91.    }
  92.  
  93.    TEST(StreaminMaxIntWritesCorrectCharacters)
  94.    {
  95.       MemoryOutStream stream;
  96.       stream << INT_MAX;
  97.       CHECK_EQUAL(maxSignedIntegralStr(sizeof(int)), stream.GetText());
  98.    }
  99.  
  100.    TEST(StreamingMinIntWritesCorrectCharacters)
  101.    {
  102.       MemoryOutStream stream;
  103.       stream << INT_MIN;
  104.       CHECK_EQUAL(minSignedIntegralStr(sizeof(int)), stream.GetText());
  105.    }
  106.  
  107.    TEST(StreamingUnsignedIntWritesCorrectCharacters)
  108.    {
  109.       MemoryOutStream stream;
  110.       stream << (unsigned int)123;
  111.       CHECK_EQUAL("123", stream.GetText());
  112.    }
  113.  
  114.    TEST(StreamingMaxUnsignedIntWritesCorrectCharacters)
  115.    {
  116.       MemoryOutStream stream;
  117.       stream << (unsigned int)UINT_MAX;
  118.       CHECK_EQUAL(maxUnsignedIntegralStr(sizeof(unsigned int)), stream.GetText());
  119.    }
  120.  
  121.    TEST(StreamingMinUnsignedIntWritesCorrectCharacters)
  122.    {
  123.       MemoryOutStream stream;
  124.       stream << (unsigned int)0;
  125.       CHECK_EQUAL("0", stream.GetText());
  126.    }
  127.  
  128.    TEST(StreamingLongWritesCorrectCharacters)
  129.    {
  130.       MemoryOutStream stream;
  131.       stream << (long)(-123);
  132.       CHECK_EQUAL("-123", stream.GetText());
  133.    }
  134.  
  135.    TEST(StreamingMaxLongWritesCorrectCharacters)
  136.    {
  137.       MemoryOutStream stream;
  138.       stream << (long)(LONG_MAX);
  139.       CHECK_EQUAL(maxSignedIntegralStr(sizeof(long)), stream.GetText());
  140.    }
  141.  
  142.    TEST(StreamingMinLongWritesCorrectCharacters)
  143.    {
  144.       MemoryOutStream stream;
  145.       stream << (long)(LONG_MIN);
  146.       CHECK_EQUAL(minSignedIntegralStr(sizeof(long)), stream.GetText());
  147.    }
  148.  
  149.    TEST(StreamingUnsignedLongWritesCorrectCharacters)
  150.    {
  151.       MemoryOutStream stream;
  152.       stream << (unsigned long)123;
  153.       CHECK_EQUAL("123", stream.GetText());
  154.    }
  155.  
  156.    TEST(StreamingMaxUnsignedLongWritesCorrectCharacters)
  157.    {
  158.       MemoryOutStream stream;
  159.       stream << (unsigned long)ULONG_MAX;
  160.       CHECK_EQUAL(maxUnsignedIntegralStr(sizeof(unsigned long)), stream.GetText());
  161.    }
  162.  
  163.    TEST(StreamingMinUnsignedLongWritesCorrectCharacters)
  164.    {
  165.       MemoryOutStream stream;
  166.       stream << (unsigned long)0ul;
  167.       CHECK_EQUAL("0", stream.GetText());
  168.    }
  169.  
  170.    TEST(StreamingLongLongWritesCorrectCharacters)
  171.    {
  172.       MemoryOutStream stream;
  173. #ifdef UNITTEST_COMPILER_IS_MSVC6
  174.       stream << (__int64)-12345i64;
  175. #else
  176.       stream << (long long)-12345ll;
  177. #endif
  178.       CHECK_EQUAL("-12345", stream.GetText());
  179.    }
  180.  
  181. #ifdef LLONG_MAX
  182.    TEST(StreamingMaxLongLongWritesCorrectCharacters)
  183.    {
  184.       MemoryOutStream stream;
  185.       stream << (long long)LLONG_MAX;
  186.       CHECK_EQUAL(maxSignedIntegralStr(sizeof(long long)), stream.GetText());
  187.    }
  188. #endif
  189.  
  190. #ifdef LLONG_MIN
  191.    TEST(StreamingMinLongLongWritesCorrectCharacters)
  192.    {
  193.       MemoryOutStream stream;
  194.       stream << (long long)LLONG_MIN;
  195.       CHECK_EQUAL(minSignedIntegralStr(sizeof(long long)), stream.GetText());
  196.    }
  197. #endif
  198.  
  199.    TEST(StreamingUnsignedLongLongWritesCorrectCharacters)
  200.    {
  201.       MemoryOutStream stream;
  202. #ifdef UNITTEST_COMPILER_IS_MSVC6
  203.       stream << (unsigned __int64)85899ui64;
  204. #else
  205.       stream << (unsigned long long)85899ull;
  206. #endif
  207.       CHECK_EQUAL("85899", stream.GetText());
  208.    }
  209.  
  210. #ifdef ULLONG_MAX
  211.    TEST(StreamingMaxUnsignedLongLongWritesCorrectCharacters)
  212.    {
  213.       MemoryOutStream stream;
  214.       stream << (unsigned long long)ULLONG_MAX;
  215.       CHECK_EQUAL(maxUnsignedIntegralStr(sizeof(unsigned long long)), stream.GetText());
  216.    }
  217. #endif
  218.  
  219.    TEST(StreamingMinUnsignedLongLongWritesCorrectCharacters)
  220.    {
  221.       MemoryOutStream stream;
  222. #ifdef UNITTEST_COMPILER_IS_MSVC6
  223.       stream << (unsigned __int64)0ui64;
  224. #else
  225.       stream << (unsigned long long)0ull;
  226. #endif
  227.       CHECK_EQUAL("0", stream.GetText());
  228.    }
  229.  
  230.    TEST(StreamingFloatWritesCorrectCharacters)
  231.    {
  232.       MemoryOutStream stream;
  233.       stream << 3.1415f;
  234.       CHECK(strstr(stream.GetText(), "3.1415"));
  235.    }
  236.  
  237.    TEST(StreamingDoubleWritesCorrectCharacters)
  238.    {
  239.       MemoryOutStream stream;
  240.       stream << 3.1415;
  241.       CHECK(strstr(stream.GetText(), "3.1415"));
  242.    }
  243.  
  244.    TEST(StreamingPointerWritesCorrectCharacters)
  245.    {
  246.       MemoryOutStream stream;
  247.       int* p = (int*)0x1234;
  248.       stream << p;
  249.       CHECK(strstr(stream.GetText(), "1234"));
  250.    }
  251.  
  252.    TEST(StreamingSizeTWritesCorrectCharacters)
  253.    {
  254.       MemoryOutStream stream;
  255.       size_t const s = 53124;
  256.       stream << s;
  257.       CHECK_EQUAL("53124", stream.GetText());
  258.    }
  259.  
  260.    TEST(ClearEmptiesMemoryOutStreamContents)
  261.    {
  262.       MemoryOutStream stream;
  263.       stream << "Hello world";
  264.       stream.Clear();
  265.       CHECK_EQUAL("", stream.GetText());
  266.    }
  267.  
  268. #ifndef UNITTEST_MEMORYOUTSTREAM_IS_STD_OSTRINGSTREAM
  269.  
  270.    TEST(StreamInitialCapacityIsCorrect)
  271.    {
  272.       MemoryOutStream stream(MemoryOutStream::GROW_CHUNK_SIZE);
  273.       CHECK_EQUAL((int)MemoryOutStream::GROW_CHUNK_SIZE, stream.GetCapacity());
  274.    }
  275.  
  276.    TEST(StreamInitialCapacityIsMultipleOfGrowChunkSize)
  277.    {
  278.       MemoryOutStream stream(MemoryOutStream::GROW_CHUNK_SIZE + 1);
  279.       CHECK_EQUAL((int)MemoryOutStream::GROW_CHUNK_SIZE * 2, stream.GetCapacity());
  280.    }
  281.  
  282.  
  283.    TEST(ExceedingCapacityGrowsBuffer)
  284.    {
  285.       MemoryOutStream stream(MemoryOutStream::GROW_CHUNK_SIZE);
  286.       stream << "012345678901234567890123456789";
  287.       char const* const oldBuffer = stream.GetText();
  288.       stream << "0123456789";
  289.       CHECK(oldBuffer != stream.GetText());
  290.    }
  291.  
  292.    TEST(ExceedingCapacityGrowsBufferByGrowChunk)
  293.    {
  294.       MemoryOutStream stream(MemoryOutStream::GROW_CHUNK_SIZE);
  295.       stream << "0123456789012345678901234567890123456789";
  296.       CHECK_EQUAL(MemoryOutStream::GROW_CHUNK_SIZE * 2, stream.GetCapacity());
  297.    }
  298.  
  299.    TEST(WritingStringLongerThanCapacityFitsInNewBuffer)
  300.    {
  301.       MemoryOutStream stream(8);
  302.       stream << "0123456789ABCDEF";
  303.       CHECK_EQUAL("0123456789ABCDEF", stream.GetText());
  304.    }
  305.  
  306.    TEST(WritingIntLongerThanCapacityFitsInNewBuffer)
  307.    {
  308.       MemoryOutStream stream(8);
  309.       stream << "aaaa" << 123456;
  310.       CHECK_EQUAL("aaaa123456", stream.GetText());
  311.    }
  312.  
  313.    TEST(WritingFloatLongerThanCapacityFitsInNewBuffer)
  314.    {
  315.       MemoryOutStream stream(8);
  316.       stream << "aaaa" << 123456.0f;
  317.       CHECK_EQUAL("aaaa123456.000000", stream.GetText());
  318.    }
  319.  
  320.    TEST(WritingSizeTLongerThanCapacityFitsInNewBuffer)
  321.    {
  322.       MemoryOutStream stream(8);
  323.       stream << "aaaa" << size_t(32145);
  324.       CHECK_EQUAL("aaaa32145", stream.GetText());
  325.    }
  326.  
  327.    TEST(VerifyLargeDoubleCanBeStreamedWithoutCrashing)
  328.    {
  329.       MemoryOutStream stream(8);
  330.       stream << DBL_MAX;
  331.       CHECK(true);
  332.    }
  333.  
  334. #endif
  335.  
  336. }
  337.