1 /* Copyright (C) 2003 Scott Wheeler <wheeler@kde.org>
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions
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.
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.
25 #include <cppunit/extensions/HelperMacros.h>
29 using namespace TagLib;
31 class TestString : public CppUnit::TestFixture
33 CPPUNIT_TEST_SUITE(TestString);
34 CPPUNIT_TEST(testString);
35 CPPUNIT_TEST(testUTF16Encode);
36 CPPUNIT_TEST(testUTF16Decode);
37 CPPUNIT_TEST(testUTF16DecodeInvalidBOM);
38 CPPUNIT_TEST(testUTF16DecodeEmptyWithBOM);
39 CPPUNIT_TEST_SUITE_END();
45 String s = "taglib string";
46 ByteVector v = "taglib string";
47 CPPUNIT_ASSERT(v == s.data(String::Latin1));
49 char str[] = "taglib string";
50 CPPUNIT_ASSERT(strcmp(s.toCString(), str) == 0);
52 String unicode("José Carlos", String::UTF8);
53 CPPUNIT_ASSERT(strcmp(unicode.toCString(), "Jos\xe9 Carlos") == 0);
55 String latin = "Jos\xe9 Carlos";
56 CPPUNIT_ASSERT(strcmp(latin.toCString(true), "José Carlos") == 0);
58 String unicode2(unicode.to8Bit(true), String::UTF8);
59 CPPUNIT_ASSERT(unicode == unicode2);
61 CPPUNIT_ASSERT(strcmp(String::number(0).toCString(), "0") == 0);
62 CPPUNIT_ASSERT(strcmp(String::number(12345678).toCString(), "12345678") == 0);
63 CPPUNIT_ASSERT(strcmp(String::number(-12345678).toCString(), "-12345678") == 0);
66 CPPUNIT_ASSERT(n.toInt() == 123);
69 CPPUNIT_ASSERT(n.toInt() == -123);
71 CPPUNIT_ASSERT(String("0").toInt() == 0);
72 CPPUNIT_ASSERT(String("1").toInt() == 1);
74 CPPUNIT_ASSERT(String(" foo ").stripWhiteSpace() == String("foo"));
75 CPPUNIT_ASSERT(String("foo ").stripWhiteSpace() == String("foo"));
76 CPPUNIT_ASSERT(String(" foo").stripWhiteSpace() == String("foo"));
78 CPPUNIT_ASSERT(memcmp(String("foo").data(String::Latin1).data(), "foo", 3) == 0);
79 CPPUNIT_ASSERT(memcmp(String("f").data(String::Latin1).data(), "f", 1) == 0);
81 ByteVector utf16 = unicode.data(String::UTF16);
83 // Check to make sure that the BOM is there and that the data size is correct
85 CPPUNIT_ASSERT(utf16.size() == 2 + (unicode.size() * 2));
87 CPPUNIT_ASSERT(unicode == String(utf16, String::UTF16));
90 void testUTF16Encode()
93 ByteVector b("\0f\0o\0o", 6);
94 ByteVector c("f\0o\0o\0", 6);
95 ByteVector d("\377\376f\0o\0o\0", 8);
96 CPPUNIT_ASSERT(a.data(String::UTF16BE) != a.data(String::UTF16LE));
97 CPPUNIT_ASSERT(b == a.data(String::UTF16BE));
98 CPPUNIT_ASSERT(c == a.data(String::UTF16LE));
99 CPPUNIT_ASSERT_EQUAL(d, a.data(String::UTF16));
102 void testUTF16Decode()
105 ByteVector b("\0f\0o\0o", 6);
106 ByteVector c("f\0o\0o\0", 6);
107 ByteVector d("\377\376f\0o\0o\0", 8);
108 CPPUNIT_ASSERT_EQUAL(a, String(b, String::UTF16BE));
109 CPPUNIT_ASSERT_EQUAL(a, String(c, String::UTF16LE));
110 CPPUNIT_ASSERT_EQUAL(a, String(d, String::UTF16));
113 // this test is expected to print "TagLib: String::prepare() -
114 // Invalid UTF16 string." on the console 3 times
115 void testUTF16DecodeInvalidBOM()
117 ByteVector b(" ", 1);
118 ByteVector c(" ", 2);
119 ByteVector d(" \0f\0o\0o", 8);
120 CPPUNIT_ASSERT_EQUAL(String(), String(b, String::UTF16));
121 CPPUNIT_ASSERT_EQUAL(String(), String(c, String::UTF16));
122 CPPUNIT_ASSERT_EQUAL(String(), String(d, String::UTF16));
125 void testUTF16DecodeEmptyWithBOM()
127 ByteVector a("\377\376", 2);
128 ByteVector b("\376\377", 2);
129 CPPUNIT_ASSERT_EQUAL(String(), String(a, String::UTF16));
130 CPPUNIT_ASSERT_EQUAL(String(), String(b, String::UTF16));
135 CPPUNIT_TEST_SUITE_REGISTRATION(TestString);