crc_ut.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include "crc.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. #include <util/stream/output.h>
  4. class TCrcTest: public TTestBase {
  5. UNIT_TEST_SUITE(TCrcTest);
  6. UNIT_TEST(TestCrc16)
  7. UNIT_TEST(TestCrc32)
  8. UNIT_TEST(TestCrc64)
  9. UNIT_TEST_SUITE_END();
  10. private:
  11. inline void TestCrc16() {
  12. const ui16 expected[] = {
  13. 41719,
  14. 57001,
  15. 53722,
  16. 3276};
  17. Run<ui16>(expected);
  18. }
  19. inline void TestCrc32() {
  20. const ui32 expected[] = {
  21. 688229491UL,
  22. 3543112608UL,
  23. 4127534015UL,
  24. 3009909774UL};
  25. Run<ui32>(expected);
  26. }
  27. inline void TestCrc64() {
  28. const ui64 expected[] = {
  29. 12116107829328640258ULL,
  30. 18186277744016380552ULL,
  31. 249923753044811734ULL,
  32. 7852471725963920356ULL};
  33. Run<ui64>(expected);
  34. }
  35. private:
  36. template <class T>
  37. inline void Run(const T* expected) {
  38. ui8 buf[256];
  39. for (size_t i = 0; i < 256; ++i) {
  40. buf[i] = i;
  41. }
  42. Test<T>(buf, 256, expected[0]);
  43. Test<T>(buf, 255, expected[1]);
  44. Test<T>(buf, 254, expected[2]);
  45. Test<T>(buf, 253, expected[3]);
  46. }
  47. template <class T>
  48. inline void Test(const void* data, size_t len, T expected) {
  49. const T res = Crc<T>(data, len);
  50. try {
  51. UNIT_ASSERT_EQUAL(res, expected);
  52. } catch (...) {
  53. Cerr << res << Endl;
  54. throw;
  55. }
  56. }
  57. };
  58. UNIT_TEST_SUITE_REGISTRATION(TCrcTest);