CRCTest.cpp 4.06 KB
Newer Older
1
2
3
/*
 * The CERN Tape Archive (CTA) project
 * Copyright (C) 2015  CERN
4
 *
5
6
7
8
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
9
10
11
12
13
14
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
15
16
17
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
18

19
#include "common/CRC.hpp"
20
21
22
23
24
25
26

#include <gtest/gtest.h>

#include <stdint.h>

namespace unitTests {

27
class cta_CRC : public ::testing::Test {
28
29
30
31
32
33
34
35
36
protected:

  virtual void SetUp() {
  }

  virtual void TearDown() {
  }
};

37
38
TEST_F(cta_CRC, testCRCRS_sw) {
  using namespace cta;
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
  
  const uint8_t block1[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43,
    47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127,
    131, 137, 139, 149, 151, 157};
  
  const uint8_t block2[] = {163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 
    223, 227, 229, 233, 239, 241, 251};

  const uint32_t computedCRC1 = crcRS_sw(0, sizeof(block1), block1);
  const uint32_t computedCRC2 = crcRS_sw(computedCRC1, sizeof(block2), block2);
  const uint32_t computedCRC3 = crcRS_sw(crcRS_sw(0, sizeof(block1), block1), 
    sizeof(block2), block2);
  
  ASSERT_EQ(computedCRC1, 0x733D4DCA);
  ASSERT_EQ(computedCRC2, 0x754ED37E);
  ASSERT_EQ(computedCRC3, 0x754ED37E);
}

57
58
TEST_F(cta_CRC, testCRC32C_sw) {
  using namespace cta;
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
  
  const uint8_t block1[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43,
    47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127,
    131, 137, 139, 149, 151, 157};
  
  const uint8_t block2[] = {163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 
    223, 227, 229, 233, 239, 241, 251};

  const uint32_t computedCRC1 = crc32c_sw(0xFFFFFFFF, sizeof(block1), block1);
  const uint32_t computedCRC2 = crc32c_sw(computedCRC1, sizeof(block2), block2);
  const uint32_t computedCRC3 = crc32c_sw(crc32c_sw(0xFFFFFFFF, sizeof(block1),
    block1), sizeof(block2), block2);
  
  ASSERT_EQ(computedCRC1, 0xE8174F48);
  ASSERT_EQ(computedCRC2, 0x56DAB0A6);
  ASSERT_EQ(computedCRC3, 0x56DAB0A6);
}
76
77
TEST_F(cta_CRC, testCRC32C_hw) {
  using namespace cta;
78
  
79
80
81
82
83
84
85
  /* check if we have SSE4_2 to test hardware CRC32C */
  int sse42;
  SSE42(sse42);
  if (sse42) {
    const uint8_t block1[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,
      43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 
      127, 131, 137, 139, 149, 151, 157};
86

87
88
89
90
91
92
93
94
95
96
97
98
99
    const uint8_t block2[] = {163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 
      223, 227, 229, 233, 239, 241, 251};

    const uint32_t computedCRC1 = crc32c_hw(0xFFFFFFFF, sizeof(block1), block1);
    const uint32_t computedCRC2 = crc32c_hw(computedCRC1, sizeof(block2),
      block2);
    const uint32_t computedCRC3 = crc32c_hw(crc32c_hw(0xFFFFFFFF, 
      sizeof(block1), block1), sizeof(block2), block2);

    ASSERT_EQ(computedCRC1, 0xE8174F48);
    ASSERT_EQ(computedCRC2, 0x56DAB0A6);
    ASSERT_EQ(computedCRC3, 0x56DAB0A6);
  }
100
}
101
102
TEST_F(cta_CRC, testCRC32C) {
  using namespace cta;
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
  
  const uint8_t block1[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43,
    47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127,
    131, 137, 139, 149, 151, 157};
  
  const uint8_t block2[] = {163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 
    223, 227, 229, 233, 239, 241, 251};

  const uint32_t computedCRC1 = crc32c(0xFFFFFFFF, sizeof(block1), block1);
  const uint32_t computedCRC2 = crc32c(computedCRC1, sizeof(block2), block2);
  const uint32_t computedCRC3 = crc32c(crc32c(0xFFFFFFFF, sizeof(block1),
    block1), sizeof(block2), block2);
  
  ASSERT_EQ(computedCRC1, 0xE8174F48);
  ASSERT_EQ(computedCRC2, 0x56DAB0A6);
  ASSERT_EQ(computedCRC3, 0x56DAB0A6);
}
} // namespace unitTests