Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <sal/types.h>
21 : #include <cppunit/TestFixture.h>
22 : #include <cppunit/extensions/HelperMacros.h>
23 : #include <cppunit/plugin/TestPlugIn.h>
24 :
25 : #include <rtl/crc.h>
26 :
27 : namespace rtl_CRC32
28 : {
29 :
30 18 : class test : public CppUnit::TestFixture
31 : {
32 : public:
33 : // initialise your test code values here.
34 6 : void setUp() SAL_OVERRIDE
35 : {
36 6 : }
37 :
38 6 : void tearDown() SAL_OVERRIDE
39 : {
40 6 : }
41 :
42 : // insert your test code here.
43 1 : void rtl_crc32_001()
44 : {
45 : // this is demonstration code
46 : // CPPUNIT_ASSERT_MESSAGE("a message", 1 == 1);
47 :
48 1 : sal_uInt32 nCRC = 0;
49 :
50 1 : char buf[] = {0};
51 1 : int num = 0;
52 :
53 1 : nCRC = rtl_crc32(nCRC, buf, num);
54 :
55 1 : CPPUNIT_ASSERT_MESSAGE("empty crc buffer", nCRC == 0);
56 1 : }
57 :
58 1 : void rtl_crc32_002()
59 : {
60 1 : sal_uInt32 nCRC = 0;
61 :
62 1 : char buf[] = {0,0};
63 1 : int num = sizeof(buf);
64 :
65 1 : nCRC = rtl_crc32(nCRC, buf, num);
66 :
67 1 : CPPUNIT_ASSERT_MESSAGE("buffer contain 2 empty bytes, crc is zero", nCRC != 0);
68 1 : }
69 :
70 1 : void rtl_crc32_002_1()
71 : {
72 1 : sal_uInt32 nCRC = 0;
73 :
74 1 : char buf[] = {0,0,0};
75 1 : int num = sizeof(buf);
76 :
77 1 : nCRC = rtl_crc32(nCRC, buf, num);
78 :
79 1 : CPPUNIT_ASSERT_MESSAGE("buffer contain 3 empty bytes, crc is zero", nCRC != 0);
80 1 : }
81 :
82 : /**
83 : * crc32 check:
84 : * Build checksum on two buffers with same size but different content,
85 : * the result (crc32 checksum) must differ
86 : */
87 :
88 1 : void rtl_crc32_003()
89 : {
90 1 : sal_uInt32 nCRC1 = 0;
91 1 : char buf1[] = {2};
92 1 : int num1 = sizeof(buf1);
93 :
94 1 : nCRC1 = rtl_crc32(nCRC1, buf1, num1);
95 :
96 1 : sal_uInt32 nCRC2 = 0;
97 1 : char buf2[] = {3};
98 1 : int num2 = sizeof(buf2);
99 :
100 1 : nCRC2 = rtl_crc32(nCRC2, buf2, num2);
101 :
102 1 : CPPUNIT_ASSERT_MESSAGE("checksum should differ for buf1 and buf2", nCRC1 != nCRC2);
103 1 : }
104 :
105 : /** check if the crc32 only use as much values, as given
106 : *
107 : */
108 1 : void rtl_crc32_003_1()
109 : {
110 1 : sal_uInt32 nCRC1 = 0;
111 1 : char buf1[] = {2,1};
112 1 : int num1 = sizeof(buf1) - 1;
113 :
114 1 : nCRC1 = rtl_crc32(nCRC1, buf1, num1);
115 :
116 1 : sal_uInt32 nCRC2 = 0;
117 1 : char buf2[] = {2,2};
118 1 : int num2 = sizeof(buf2) - 1;
119 :
120 1 : nCRC2 = rtl_crc32(nCRC2, buf2, num2);
121 :
122 1 : CPPUNIT_ASSERT_MESSAGE("checksum leave it's bounds", nCRC1 == nCRC2);
123 1 : }
124 :
125 : /** check if the crc32 differ at same content in reverse order
126 : *
127 : */
128 1 : void rtl_crc32_003_2()
129 : {
130 1 : sal_uInt32 nCRC1 = 0;
131 1 : char buf1[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
132 1 : int num1 = sizeof(buf1);
133 :
134 1 : nCRC1 = rtl_crc32(nCRC1, buf1, num1);
135 :
136 1 : sal_uInt32 nCRC2 = 0;
137 1 : char buf2[] = {20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0};
138 1 : int num2 = sizeof(buf2);
139 :
140 1 : nCRC2 = rtl_crc32(nCRC2, buf2, num2);
141 :
142 1 : CPPUNIT_ASSERT_MESSAGE("checksum should differ", nCRC1 != nCRC2);
143 1 : }
144 :
145 : // Change the following lines only, if you add, remove or rename
146 : // member functions of the current class,
147 : // because these macros are need by auto register mechanism.
148 :
149 2 : CPPUNIT_TEST_SUITE(test);
150 1 : CPPUNIT_TEST(rtl_crc32_001);
151 1 : CPPUNIT_TEST(rtl_crc32_002);
152 1 : CPPUNIT_TEST(rtl_crc32_002_1);
153 1 : CPPUNIT_TEST(rtl_crc32_003);
154 1 : CPPUNIT_TEST(rtl_crc32_003_1);
155 1 : CPPUNIT_TEST(rtl_crc32_003_2);
156 5 : CPPUNIT_TEST_SUITE_END();
157 : }; // class test
158 :
159 1 : CPPUNIT_TEST_SUITE_REGISTRATION(rtl_CRC32::test);
160 : } // namespace rtl_CRC32
161 :
162 : // this macro creates an empty function, which will called by the RegisterAllFunctions()
163 : // to let the user the possibility to also register some functions by hand.
164 4 : CPPUNIT_PLUGIN_IMPLEMENT();
165 :
166 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|