PageRenderTime 72ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/commoncpp2-1.8.1/tests/ccxx_tests.cpp

#
C++ | 107 lines | 54 code | 21 blank | 32 comment | 7 complexity | 9515eb83e8d0d48a3ac4928230ce9ff3 MD5 | raw file
Possible License(s): GPL-2.0
  1. #include <cppunit/extensions/TestFactoryRegistry.h>
  2. #include <cppunit/CompilerOutputter.h>
  3. #include <cppunit/ui/text/TestRunner.h>
  4. #include <string>
  5. #include <iomanip>
  6. //#include "SHATumblerTest.h"
  7. //CPPUNIT_TEST_SUITE_REGISTRATION(SHATumblerTest);
  8. using namespace std;
  9. #define ULONG unsigned long
  10. ULONG crc32_table[256]; // Lookup table array
  11. // Reflection is a requirement for the official CRC-32 standard.
  12. // You can create CRCs without it, but they won't conform to the standard.
  13. ULONG Reflect(ULONG ref, char ch)
  14. {// Used only by Init_CRC32_Table()
  15. ULONG value(0);
  16. // Swap bit 0 for bit 7
  17. // bit 1 for bit 6, etc.
  18. for(int i = 1; i < (ch + 1); i++) {
  19. if(ref & 1)
  20. value |= 1 << (ch - i);
  21. ref >>= 1;
  22. }
  23. return value;
  24. }
  25. // Call this function only once to initialize the CRC table.
  26. void Init_CRC32_Table()
  27. {// Called by OnInitDialog()
  28. // This is the official polynomial used by CRC-32
  29. // in PKZip, WinZip and Ethernet.
  30. ULONG ulPolynomial = 0x04c11db7;
  31. // 256 values representing ASCII character codes.
  32. for(int i = 0; i <= 0xFF; i++) {
  33. crc32_table[i]=Reflect(i, 8) << 24;
  34. for (int j = 0; j < 8; j++)
  35. crc32_table[i] = (crc32_table[i] << 1) ^ (crc32_table[i] & (1 << 31) ? ulPolynomial : 0);
  36. crc32_table[i] = Reflect(crc32_table[i], 32);
  37. //cout << i << ":" << crc32_table[i] << endl;
  38. }
  39. }
  40. // Once the lookup table has been filled in by the two functions above,
  41. // this function creates all CRCs using only the lookup table.
  42. int Get_CRC(string &text)
  43. {// Called by OnChangeText()
  44. // Be sure to use unsigned variables,
  45. // because negative values introduce high bits
  46. // where zero bits are required.
  47. // Start out with all bits set high.
  48. ULONG ulCRC(0xffffffff);
  49. int len;
  50. unsigned char* buffer;
  51. // Get the length.
  52. // Note that if the text contains NULL characters
  53. // processing ends at the first NULL and the CRC value is invalid.
  54. // See the 32 Bit File Demonstration source code
  55. // for a method of dealing with NULL characters in files.
  56. len = text.length();
  57. // Save the text in the buffer.
  58. buffer = (unsigned char*)text.c_str();
  59. // Perform the algorithm on each character
  60. // in the string, using the lookup table values.
  61. while(len--)
  62. ulCRC = (ulCRC >> 8) ^ crc32_table[(ulCRC & 0xFF) ^ *buffer++];
  63. // Exclusive OR the result with the beginning value.
  64. return ulCRC ^ 0xffffffff;
  65. }
  66. int main(int argc, char * argv[])
  67. {
  68. // if command line contains "-selftest" then this is the post build check
  69. // => the output must be in the compiler error format.
  70. bool selfTest = (argc > 1) && (std::string("-selftest") == argv[1]);
  71. // new
  72. CppUnit::TestFactoryRegistry &registry = CppUnit::TestFactoryRegistry::getRegistry();
  73. CppUnit::TextUi::TestRunner runner;
  74. runner.addTest(registry.makeTest());
  75. if(selfTest)
  76. { // Change the default outputter to a compiler error format outputter
  77. // The test runner owns the new outputter.
  78. runner.setOutputter(
  79. CppUnit::CompilerOutputter::defaultOutputter(&runner.result(), std::cerr)
  80. );
  81. }
  82. bool wasSucessful = runner.run("", false);
  83. //Init_CRC32_Table();
  84. //cout << hex << setw(8) << Get_CRC(string("pippo"));
  85. return !wasSucessful; // inverted as 0 is success 1 is failure
  86. }