(file) Return to test_base.cpp CVS log (file) (dir) Up to [OMI] / omi / base / tests / Attic

Diff for /omi/base/tests/Attic/test_base.cpp between version 1.1 and 1.2

version 1.1, 2012/05/30 22:47:49 version 1.2, 2012/06/15 20:51:14
Line 37 
Line 37 
 #include <base/strarr.h> #include <base/strarr.h>
 #include <base/getopt.h> #include <base/getopt.h>
 #include <base/hashtable.h> #include <base/hashtable.h>
   #include <base/base64.h>
 #include "MSFT_AllTypes.h" #include "MSFT_AllTypes.h"
 #include "MSFT_Process.h" #include "MSFT_Process.h"
  
Line 2922 
Line 2923 
     HashTable_Destroy(&table);     HashTable_Destroy(&table);
 } }
  
   int TestBase64EncCallback(const char* data, size_t size, void* callbackData)
   {
       vector<char>& buf = *((vector<char>*)callbackData);
       buf.insert(buf.end(), data, data + size);
       return 0;
   }
   
   int TestBase64DecCallback(const void* data, size_t size, void* callbackData)
   {
       vector<unsigned char>& buf = *((vector<unsigned char>*)callbackData);
       buf.insert(buf.end(), (unsigned char*)data, (unsigned char*)data + size);
       return 0;
   }
   
   static bool TestBase64Str(
       const char* str,
       const char* expect)
   {
       vector<char> buf;
   
       int r = Base64Enc(
           str,
           strlen(str),
           TestBase64EncCallback,
           &buf);
   
       if (r != 0)
           return false;
   
       return memcmp(&buf[0], expect, buf.size()) == 0;
   }
   
   static int TestBase64Aux(const void* data, size_t size)
   {
       // Encode:
   
       vector<char> enc;
   
       if (Base64Enc(data, size, TestBase64EncCallback, &enc) != 0)
           return -1;
   
   #if 0
       printf("enc{%.*s}\n", (int)enc.size(), &enc[0]);
   #endif
   
       // Decode:
   
       vector<unsigned char> dec;
   
       if (Base64Dec(&enc[0], enc.size(), TestBase64DecCallback, &dec) != 0)
           return -1;
   
   #if 0
       printf("dec{%.*s}\n", (int)dec.size(), &dec[0]);
   #endif
   
       // Verify:
   
       if (dec.size() != size)
           return -1;
   
       if (memcmp(&dec[0], data, size) != 0)
           return -1;
   
       return 0;
   }
   
   static void TestBase64()
   {
       UT_ASSERT(TestBase64Str("a", "YQ=="));
       UT_ASSERT(TestBase64Str("ab", "YWI="));
       UT_ASSERT(TestBase64Str("abc", "YWJj"));
       UT_ASSERT(TestBase64Str("abcd", "YWJjZA=="));
       UT_ASSERT(TestBase64Str("abcde", "YWJjZGU="));
       UT_ASSERT(TestBase64Str("Hello World!", "SGVsbG8gV29ybGQh"));
       UT_ASSERT(TestBase64Str("hello", "aGVsbG8="));
       UT_ASSERT(TestBase64Str(
           "Come what, come may; time and the hour run through the roughest day",
           "Q29tZSB3aGF0LCBjb21lIG1heTsgdGltZSBhbmQgdGhlIGhvdXIgcnVuIHRocm91Z2ggdGhlIHJvdWdoZXN0IGRheQ=="));
   
       UT_ASSERT(TestBase64Aux("a", 1) == 0);
       UT_ASSERT(TestBase64Aux("ab", 2) == 0);
       UT_ASSERT(TestBase64Aux("abc", 3) == 0);
   
       // Generate random data for varying lengths:
   
       for (size_t i = 0; i < 1024; i++)
       {
           vector<unsigned char> data;
   
           for (size_t j = 0; j < i; j++)
           {
               data.push_back(rand() % 256);
           }
   
           UT_ASSERT(TestBase64Aux(&data[0], data.size()) == 0);
       }
   }
   
 static void RunTests() static void RunTests()
 { {
     UT_TEST(TestHashTable1);     UT_TEST(TestHashTable1);
Line 2962 
Line 3062 
     UT_TEST(TestGetOpt);     UT_TEST(TestGetOpt);
     UT_TEST(TestGetOptErr);     UT_TEST(TestGetOptErr);
     UT_TEST(TestMatchKeys);     UT_TEST(TestMatchKeys);
       UT_TEST(TestBase64);
     UT_ENTRY_POINT(TestWSManDatetime);     UT_ENTRY_POINT(TestWSManDatetime);
 } }
  


Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2

ViewCVS 0.9.2