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>
30
using namespace TagLib;
32
class TestString : public CppUnit::TestFixture
34
CPPUNIT_TEST_SUITE(TestString);
35
CPPUNIT_TEST(testString);
36
CPPUNIT_TEST(testUTF16Encode);
37
CPPUNIT_TEST(testUTF16Decode);
38
CPPUNIT_TEST(testUTF16DecodeInvalidBOM);
39
CPPUNIT_TEST(testUTF16DecodeEmptyWithBOM);
40
CPPUNIT_TEST_SUITE_END();
46
String s = "taglib string";
47
ByteVector v = "taglib string";
48
CPPUNIT_ASSERT(v == s.data(String::Latin1));
50
char str[] = "taglib string";
51
CPPUNIT_ASSERT(strcmp(s.toCString(), str) == 0);
53
String unicode("José Carlos", String::UTF8);
54
CPPUNIT_ASSERT(strcmp(unicode.toCString(), "Jos\xe9 Carlos") == 0);
56
String latin = "Jos\xe9 Carlos";
57
CPPUNIT_ASSERT(strcmp(latin.toCString(true), "José Carlos") == 0);
59
String unicode2(unicode.to8Bit(true), String::UTF8);
60
CPPUNIT_ASSERT(unicode == unicode2);
62
CPPUNIT_ASSERT(strcmp(String::number(0).toCString(), "0") == 0);
63
CPPUNIT_ASSERT(strcmp(String::number(12345678).toCString(), "12345678") == 0);
64
CPPUNIT_ASSERT(strcmp(String::number(-12345678).toCString(), "-12345678") == 0);
67
CPPUNIT_ASSERT(n.toInt() == 123);
70
CPPUNIT_ASSERT(n.toInt() == -123);
72
CPPUNIT_ASSERT(String("0").toInt() == 0);
73
CPPUNIT_ASSERT(String("1").toInt() == 1);
75
CPPUNIT_ASSERT(String(" foo ").stripWhiteSpace() == String("foo"));
76
CPPUNIT_ASSERT(String("foo ").stripWhiteSpace() == String("foo"));
77
CPPUNIT_ASSERT(String(" foo").stripWhiteSpace() == String("foo"));
79
CPPUNIT_ASSERT(memcmp(String("foo").data(String::Latin1).data(), "foo", 3) == 0);
80
CPPUNIT_ASSERT(memcmp(String("f").data(String::Latin1).data(), "f", 1) == 0);
82
ByteVector utf16 = unicode.data(String::UTF16);
84
// Check to make sure that the BOM is there and that the data size is correct
86
CPPUNIT_ASSERT(utf16.size() == 2 + (unicode.size() * 2));
88
CPPUNIT_ASSERT(unicode == String(utf16, String::UTF16));
91
void testUTF16Encode()
94
ByteVector b("\0f\0o\0o", 6);
95
ByteVector c("f\0o\0o\0", 6);
96
ByteVector d("\377\376f\0o\0o\0", 8);
97
CPPUNIT_ASSERT(a.data(String::UTF16BE) != a.data(String::UTF16LE));
98
CPPUNIT_ASSERT(b == a.data(String::UTF16BE));
99
CPPUNIT_ASSERT(c == a.data(String::UTF16LE));
100
CPPUNIT_ASSERT_EQUAL(d, a.data(String::UTF16));
103
void testUTF16Decode()
106
ByteVector b("\0f\0o\0o", 6);
107
ByteVector c("f\0o\0o\0", 6);
108
ByteVector d("\377\376f\0o\0o\0", 8);
109
CPPUNIT_ASSERT_EQUAL(a, String(b, String::UTF16BE));
110
CPPUNIT_ASSERT_EQUAL(a, String(c, String::UTF16LE));
111
CPPUNIT_ASSERT_EQUAL(a, String(d, String::UTF16));
114
// this test is expected to print "TagLib: String::prepare() -
115
// Invalid UTF16 string." on the console 3 times
116
void testUTF16DecodeInvalidBOM()
118
ByteVector b(" ", 1);
119
ByteVector c(" ", 2);
120
ByteVector d(" \0f\0o\0o", 8);
121
CPPUNIT_ASSERT_EQUAL(String(), String(b, String::UTF16));
122
CPPUNIT_ASSERT_EQUAL(String(), String(c, String::UTF16));
123
CPPUNIT_ASSERT_EQUAL(String(), String(d, String::UTF16));
126
void testUTF16DecodeEmptyWithBOM()
128
ByteVector a("\377\376", 2);
129
ByteVector b("\376\377", 2);
130
CPPUNIT_ASSERT_EQUAL(String(), String(a, String::UTF16));
131
CPPUNIT_ASSERT_EQUAL(String(), String(b, String::UTF16));
136
CPPUNIT_TEST_SUITE_REGISTRATION(TestString);