Avoid potential negative array index access to cached text.
[LibreOffice.git] / tools / qa / cppunit / test_guid.cxx
blobf16eb389319c60b51d39d6fa01ea970ddd753e96
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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 */
10 #include <sal/types.h>
11 #include <cppunit/TestFixture.h>
12 #include <cppunit/extensions/HelperMacros.h>
13 #include <tools/Guid.hxx>
15 namespace tools
17 class GuidTest : public CppUnit::TestFixture
19 public:
20 void testGetString()
22 sal_uInt8 pArray[16] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
23 Guid aGuid(pArray);
24 CPPUNIT_ASSERT_EQUAL("{01020304-0506-0708-090A-0B0C0D0E0F10}"_ostr, aGuid.getString());
27 void testCreate()
29 // data is generated when Guid is created
30 Guid aGuid1(Guid::Generate);
32 // check it's not initialized to 0
33 CPPUNIT_ASSERT(*std::max_element(aGuid1.begin(), aGuid1.end()) > 0u);
35 // data is generated when Guid is created
36 Guid aGuid2(Guid::Generate);
38 CPPUNIT_ASSERT_EQUAL(aGuid1, aGuid1);
39 CPPUNIT_ASSERT_EQUAL(aGuid2, aGuid2);
41 CPPUNIT_ASSERT(aGuid1 != aGuid2);
44 void testParse()
46 sal_uInt8 pArray1[16] = { 1, 1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 5, 5, 5, 5 };
47 Guid aGuid1(pArray1);
49 Guid aGuid2("{01010101-0202-0303-0404-050505050505}");
50 CPPUNIT_ASSERT_EQUAL(aGuid1, aGuid2);
52 sal_uInt8 pArray2[16] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
53 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
54 Guid aGuid3(pArray2);
56 Guid aGuid4("{FFffFFff-FFff-FFff-FFff-FFffFFffFFff}");
57 CPPUNIT_ASSERT_EQUAL(aGuid3, aGuid4);
59 Guid aGuid5("{FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF}");
60 CPPUNIT_ASSERT_EQUAL(aGuid5, aGuid4);
62 Guid aGuid6("01010101-0202-0303-0404-0505050505005");
63 CPPUNIT_ASSERT(aGuid6.isEmpty());
65 Guid aGuid7("Random");
66 CPPUNIT_ASSERT(aGuid7.isEmpty());
68 Guid aGuid8("{0G010101-0202-0303-0404-050505050505}");
69 CPPUNIT_ASSERT(aGuid8.isEmpty());
71 Guid aGuid9("{FFAAFFAA-FFAA-FFAA-FFAA-FF00FF11FF22}");
72 CPPUNIT_ASSERT(!aGuid9.isEmpty());
74 Guid aGuid10("{FFAAFFAA?FFAA-FFAA-FFAA-FF00FF11FF22}");
75 CPPUNIT_ASSERT(aGuid10.isEmpty());
78 void testEmpty()
80 sal_uInt8 pArray1[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
81 Guid aGuid1(pArray1);
82 CPPUNIT_ASSERT(aGuid1.isEmpty());
84 sal_uInt8 pArray2[16] = { 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
85 Guid aGuid2(pArray2);
86 CPPUNIT_ASSERT(!aGuid2.isEmpty());
88 Guid aGuid3;
89 CPPUNIT_ASSERT(aGuid3.isEmpty());
92 void testCopyAndAssign()
94 sal_uInt8 pArray1[16] = { 1, 1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 5, 5, 5, 5 };
95 Guid aGuid1(pArray1);
97 // test copy constructor
98 Guid aGuid2(aGuid1);
99 CPPUNIT_ASSERT_EQUAL(aGuid1, aGuid2);
100 CPPUNIT_ASSERT(std::equal(aGuid1.cbegin(), aGuid1.cend(), aGuid2.cbegin(), aGuid2.cend()));
102 // test assign
103 Guid aGuid3 = aGuid1;
104 CPPUNIT_ASSERT_EQUAL(aGuid3, aGuid1);
105 CPPUNIT_ASSERT(std::equal(aGuid3.cbegin(), aGuid3.cend(), aGuid1.cbegin(), aGuid1.cend()));
106 CPPUNIT_ASSERT_EQUAL(aGuid3, aGuid2);
107 CPPUNIT_ASSERT(std::equal(aGuid3.cbegin(), aGuid3.cend(), aGuid2.cbegin(), aGuid2.cend()));
110 CPPUNIT_TEST_SUITE(GuidTest);
111 CPPUNIT_TEST(testGetString);
112 CPPUNIT_TEST(testCreate);
113 CPPUNIT_TEST(testParse);
114 CPPUNIT_TEST(testEmpty);
115 CPPUNIT_TEST(testCopyAndAssign);
116 CPPUNIT_TEST_SUITE_END();
119 CPPUNIT_TEST_SUITE_REGISTRATION(GuidTest);
121 } // namespace tools
123 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */