xref: /trunk/main/sal/qa/rtl/crc32/rtl_crc32.cxx (revision 38699c9b)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 
25 // MARKER(update_precomp.py): autogen include statement, do not remove
26 #include "precompiled_sal.hxx"
27 // autogenerated file with codegen.pl
28 
29 #include "gtest/gtest.h"
30 #include <rtl/crc.h>
31 
32 namespace rtl_CRC32
33 {
34 
35 class test : public ::testing::Test
36 {
37 public:
38     // initialise your test code values here.
SetUp()39     void SetUp()
40     {
41     }
42 
TearDown()43     void TearDown()
44     {
45     }
46 }; // class test
47 
TEST_F(test,rtl_crc32_001)48 TEST_F(test, rtl_crc32_001)
49 {
50     sal_uInt32 nCRC = 0;
51 
52     char buf[] = {0};
53     int num = 0;
54 
55     nCRC = rtl_crc32(nCRC, buf, num);
56 
57     ASSERT_TRUE(nCRC == 0) << "empty crc buffer";
58 }
59 
TEST_F(test,rtl_crc32_002)60 TEST_F(test, rtl_crc32_002)
61 {
62     sal_uInt32 nCRC = 0;
63 
64     char buf[] = {0,0};
65     int num = sizeof(buf);
66 
67     nCRC = rtl_crc32(nCRC, buf, num);
68 
69     ASSERT_TRUE(nCRC != 0) << "buffer contain 2 empty bytes, crc is zero";
70 }
71 
TEST_F(test,rtl_crc32_002_1)72 TEST_F(test, rtl_crc32_002_1)
73 {
74     sal_uInt32 nCRC = 0;
75 
76     char buf[] = {0,0,0};
77     int num = sizeof(buf);
78 
79     nCRC = rtl_crc32(nCRC, buf, num);
80 
81     ASSERT_TRUE(nCRC != 0) << "buffer contain 3 empty bytes, crc is zero";
82 }
83 
84 /**
85  * crc32 check:
86  * Build checksum on two buffers with same size but different content,
87  * the result (crc32 checksum) must differ
88  */
89 
TEST_F(test,rtl_crc32_003)90 TEST_F(test, rtl_crc32_003)
91 {
92     sal_uInt32 nCRC1 = 0;
93     char buf1[] = {2};
94     int num1 = sizeof(buf1);
95 
96     nCRC1 = rtl_crc32(nCRC1, buf1, num1);
97 
98     sal_uInt32 nCRC2 = 0;
99     char buf2[] = {3};
100     int num2 = sizeof(buf2);
101 
102     nCRC2 = rtl_crc32(nCRC2, buf2, num2);
103 
104     ASSERT_TRUE(nCRC1 != nCRC2) << "checksum should differ for buf1 and buf2";
105 }
106 
107 /** check if the crc32 only use as much values, as given
108  *
109  */
TEST_F(test,rtl_crc32_003_1)110 TEST_F(test, rtl_crc32_003_1)
111 {
112     sal_uInt32 nCRC1 = 0;
113     char buf1[] = {2,1};
114     int num1 = sizeof(buf1) - 1;
115 
116     nCRC1 = rtl_crc32(nCRC1, buf1, num1);
117 
118     sal_uInt32 nCRC2 = 0;
119     char buf2[] = {2,2};
120     int num2 = sizeof(buf2) - 1;
121 
122     nCRC2 = rtl_crc32(nCRC2, buf2, num2);
123 
124     ASSERT_TRUE(nCRC1 == nCRC2) << "checksum leave it's bounds";
125 }
126 
127 /** check if the crc32 differ at same content in reverse order
128  *
129  */
TEST_F(test,rtl_crc32_003_2)130 TEST_F(test, rtl_crc32_003_2)
131 {
132     sal_uInt32 nCRC1 = 0;
133     char buf1[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
134     int num1 = sizeof(buf1);
135 
136     nCRC1 = rtl_crc32(nCRC1, buf1, num1);
137 
138     sal_uInt32 nCRC2 = 0;
139     char buf2[] = {20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0};
140     int num2 = sizeof(buf2);
141 
142     nCRC2 = rtl_crc32(nCRC2, buf2, num2);
143 
144     ASSERT_TRUE(nCRC1 != nCRC2) << "checksum should differ";
145 }
146 
147 // -----------------------------------------------------------------------------
148 } // namespace rtl_CRC32
149 
main(int argc,char ** argv)150 int main(int argc, char **argv)
151 {
152     ::testing::InitGoogleTest(&argc, argv);
153     return RUN_ALL_TESTS();
154 }
155