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