Tweak colouring in the processor list.
[ardour.git] / libs / taglib / tests / test_bytevector.cpp
1 /* Copyright (C) 2003 Scott Wheeler <wheeler@kde.org>
2  *
3  * Redistribution and use in source and binary forms, with or without
4  * modification, are permitted provided that the following conditions
5  * are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24
25 #include <cppunit/extensions/HelperMacros.h>
26 #include <tbytevector.h>
27 #include <tbytevectorlist.h>
28
29 using namespace std;
30 using namespace TagLib;
31
32 class TestByteVector : public CppUnit::TestFixture
33 {
34   CPPUNIT_TEST_SUITE(TestByteVector);
35   CPPUNIT_TEST(testByteVector);
36   CPPUNIT_TEST(testFind1);
37   CPPUNIT_TEST(testFind2);
38   CPPUNIT_TEST(testRfind1);
39   CPPUNIT_TEST(testRfind2);
40   CPPUNIT_TEST_SUITE_END();
41
42 public:
43
44   void testConversion(unsigned int i, unsigned char a, unsigned char b, unsigned char c, unsigned char d)
45   {
46     ByteVector v(4, 0);
47
48     v[3] = a;
49     v[2] = b;
50     v[1] = c;
51     v[0] = d;
52     CPPUNIT_ASSERT(v.toUInt(false) == i);
53
54     v[0] = a;
55     v[1] = b;
56     v[2] = c;
57     v[3] = d;
58     CPPUNIT_ASSERT(v.toUInt() == i);
59   }
60
61   void testByteVector()
62   {
63     ByteVector v("foobar");
64
65     CPPUNIT_ASSERT(v.find("ob") == 2);
66     CPPUNIT_ASSERT(v.find('b') == 3);
67
68     ByteVector n(4, 0);
69     n[0] = 1;
70     CPPUNIT_ASSERT(n.toUInt(true) == 16777216);
71     CPPUNIT_ASSERT(n.toUInt(false) == 1);
72     CPPUNIT_ASSERT(ByteVector::fromUInt(16777216, true) == n);
73     CPPUNIT_ASSERT(ByteVector::fromUInt(1, false) == n);
74
75     CPPUNIT_ASSERT(ByteVector::fromUInt(0xa0).toUInt() == 0xa0);
76
77     testConversion(0x000000a0, 0x00, 0x00, 0x00, 0xa0);
78     testConversion(0xd50bf072, 0xd5, 0x0b, 0xf0, 0x72);
79
80     ByteVector intVector(2, 0);
81     intVector[0] = char(0xfc);
82     intVector[1] = char(0x00);
83     CPPUNIT_ASSERT(intVector.toShort() == -1024);
84     intVector[0] = char(0x04);
85     intVector[1] = char(0x00);
86     CPPUNIT_ASSERT(intVector.toShort() == 1024);
87
88     CPPUNIT_ASSERT(ByteVector::fromLongLong(1).toLongLong() == 1);
89     CPPUNIT_ASSERT(ByteVector::fromLongLong(0).toLongLong() == 0);
90     CPPUNIT_ASSERT(ByteVector::fromLongLong(0xffffffffffffffffLL).toLongLong() == -1);
91     CPPUNIT_ASSERT(ByteVector::fromLongLong(0xfffffffffffffffeLL).toLongLong() == -2);
92     CPPUNIT_ASSERT(ByteVector::fromLongLong(1024).toLongLong() == 1024);
93
94     ByteVector a1("foo");
95     a1.append("bar");
96     CPPUNIT_ASSERT(a1 == "foobar");
97
98     ByteVector a2("foo");
99     a2.append("b");
100     CPPUNIT_ASSERT(a2 == "foob");
101
102     ByteVector a3;
103     a3.append("b");
104     CPPUNIT_ASSERT(a3 == "b");
105
106     ByteVector s1("foo");
107     CPPUNIT_ASSERT(ByteVectorList::split(s1, " ").size() == 1);
108
109     ByteVector s2("f");
110     CPPUNIT_ASSERT(ByteVectorList::split(s2, " ").size() == 1);
111
112
113     CPPUNIT_ASSERT(ByteVector().size() == 0);
114     CPPUNIT_ASSERT(ByteVector("asdf").clear().size() == 0);
115     CPPUNIT_ASSERT(ByteVector("asdf").clear() == ByteVector());
116   }
117
118   void testFind1()
119   {
120     CPPUNIT_ASSERT_EQUAL(4, ByteVector("....SggO."). find("SggO"));
121     CPPUNIT_ASSERT_EQUAL(4, ByteVector("....SggO."). find("SggO", 0));
122     CPPUNIT_ASSERT_EQUAL(4, ByteVector("....SggO."). find("SggO", 1));
123     CPPUNIT_ASSERT_EQUAL(4, ByteVector("....SggO."). find("SggO", 2));
124     CPPUNIT_ASSERT_EQUAL(4, ByteVector("....SggO."). find("SggO", 3));
125     CPPUNIT_ASSERT_EQUAL(4, ByteVector("....SggO."). find("SggO", 4));
126     CPPUNIT_ASSERT_EQUAL(-1, ByteVector("....SggO."). find("SggO", 5));
127     CPPUNIT_ASSERT_EQUAL(-1, ByteVector("....SggO."). find("SggO", 6));
128     CPPUNIT_ASSERT_EQUAL(-1, ByteVector("....SggO."). find("SggO", 7));
129     CPPUNIT_ASSERT_EQUAL(-1, ByteVector("....SggO."). find("SggO", 8));
130   }
131
132   void testFind2()
133   {
134     CPPUNIT_ASSERT_EQUAL(0, ByteVector("\x01", 1).find("\x01"));
135     CPPUNIT_ASSERT_EQUAL(0, ByteVector("\x01\x02", 2).find("\x01\x02"));
136     CPPUNIT_ASSERT_EQUAL(-1, ByteVector("\x01", 1).find("\x02"));
137     CPPUNIT_ASSERT_EQUAL(-1, ByteVector("\x01\x02", 2).find("\x01\x03"));
138   }
139
140   void testRfind1()
141   {
142     CPPUNIT_ASSERT_EQUAL(1, ByteVector(".OggS....").rfind("OggS", 0));
143     CPPUNIT_ASSERT_EQUAL(1, ByteVector(".OggS....").rfind("OggS", 1));
144     CPPUNIT_ASSERT_EQUAL(1, ByteVector(".OggS....").rfind("OggS", 2));
145     CPPUNIT_ASSERT_EQUAL(1, ByteVector(".OggS....").rfind("OggS", 3));
146     CPPUNIT_ASSERT_EQUAL(1, ByteVector(".OggS....").rfind("OggS", 4));
147     CPPUNIT_ASSERT_EQUAL(1, ByteVector(".OggS....").rfind("OggS", 5));
148     CPPUNIT_ASSERT_EQUAL(1, ByteVector(".OggS....").rfind("OggS", 6));
149     CPPUNIT_ASSERT_EQUAL(1, ByteVector(".OggS....").rfind("OggS", 7));
150     CPPUNIT_ASSERT_EQUAL(1, ByteVector(".OggS....").rfind("OggS", 8));
151     CPPUNIT_ASSERT_EQUAL(1, ByteVector(".OggS....").rfind("OggS"));
152   }
153
154   void testRfind2()
155   {
156     ByteVector r0("**************");
157     ByteVector r1("OggS**********");
158     ByteVector r2("**********OggS");
159     ByteVector r3("OggS******OggS");
160     ByteVector r4("OggS*OggS*OggS");
161
162     CPPUNIT_ASSERT_EQUAL(-1, r0.find("OggS"));
163     CPPUNIT_ASSERT_EQUAL(-1, r0.rfind("OggS"));
164     CPPUNIT_ASSERT_EQUAL(0, r1.find("OggS"));
165     CPPUNIT_ASSERT_EQUAL(0, r1.rfind("OggS"));
166     CPPUNIT_ASSERT_EQUAL(10, r2.find("OggS"));
167     CPPUNIT_ASSERT_EQUAL(10, r2.rfind("OggS"));
168     CPPUNIT_ASSERT_EQUAL(0, r3.find("OggS"));
169     CPPUNIT_ASSERT_EQUAL(10, r3.rfind("OggS"));
170     CPPUNIT_ASSERT_EQUAL(10, r4.rfind("OggS"));
171     CPPUNIT_ASSERT_EQUAL(10, r4.rfind("OggS", 0));
172     CPPUNIT_ASSERT_EQUAL(5, r4.rfind("OggS", 7));
173     CPPUNIT_ASSERT_EQUAL(10, r4.rfind("OggS", 12));
174   }
175
176 };
177
178 CPPUNIT_TEST_SUITE_REGISTRATION(TestByteVector);