~njh-aelius/maxosx/musicbrainz-tags

« back to all changes in this revision

Viewing changes to Frameworks/taglib/taglib/tests/test_string.cpp

  • Committer: stephen_booth
  • Date: 2008-04-30 02:09:12 UTC
  • Revision ID: svn-v4:6b6cea13-1402-0410-9567-a7afb52bf336:trunk:1372
Update to latest taglib SVN

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 <tstring.h>
 
27
#include <string.h>
 
28
 
 
29
using namespace std;
 
30
using namespace TagLib;
 
31
 
 
32
class TestString : public CppUnit::TestFixture
 
33
{
 
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();
 
41
 
 
42
public:
 
43
 
 
44
  void testString()
 
45
  {
 
46
    String s = "taglib string";
 
47
    ByteVector v = "taglib string";
 
48
    CPPUNIT_ASSERT(v == s.data(String::Latin1));
 
49
 
 
50
    char str[] = "taglib string";
 
51
    CPPUNIT_ASSERT(strcmp(s.toCString(), str) == 0);
 
52
 
 
53
    String unicode("José Carlos", String::UTF8);
 
54
    CPPUNIT_ASSERT(strcmp(unicode.toCString(), "Jos\xe9 Carlos") == 0);
 
55
 
 
56
    String latin = "Jos\xe9 Carlos";
 
57
    CPPUNIT_ASSERT(strcmp(latin.toCString(true), "José Carlos") == 0);
 
58
 
 
59
    String unicode2(unicode.to8Bit(true), String::UTF8);
 
60
    CPPUNIT_ASSERT(unicode == unicode2);
 
61
 
 
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);
 
65
 
 
66
    String n = "123";
 
67
    CPPUNIT_ASSERT(n.toInt() == 123);
 
68
 
 
69
    n = "-123";
 
70
    CPPUNIT_ASSERT(n.toInt() == -123);
 
71
 
 
72
    CPPUNIT_ASSERT(String("0").toInt() == 0);
 
73
    CPPUNIT_ASSERT(String("1").toInt() == 1);
 
74
 
 
75
    CPPUNIT_ASSERT(String("  foo  ").stripWhiteSpace() == String("foo"));
 
76
    CPPUNIT_ASSERT(String("foo    ").stripWhiteSpace() == String("foo"));
 
77
    CPPUNIT_ASSERT(String("    foo").stripWhiteSpace() == String("foo"));
 
78
 
 
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);
 
81
 
 
82
    ByteVector utf16 = unicode.data(String::UTF16);
 
83
 
 
84
  // Check to make sure that the BOM is there and that the data size is correct
 
85
 
 
86
    CPPUNIT_ASSERT(utf16.size() == 2 + (unicode.size() * 2));
 
87
 
 
88
    CPPUNIT_ASSERT(unicode == String(utf16, String::UTF16));
 
89
  }
 
90
 
 
91
  void testUTF16Encode()
 
92
  {
 
93
    String a("foo");
 
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));
 
101
  }
 
102
 
 
103
  void testUTF16Decode()
 
104
  {
 
105
    String a("foo");
 
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));
 
112
  }
 
113
 
 
114
  // this test is expected to print "TagLib: String::prepare() -
 
115
  // Invalid UTF16 string." on the console 3 times
 
116
  void testUTF16DecodeInvalidBOM()
 
117
  {
 
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));
 
124
  }
 
125
 
 
126
  void testUTF16DecodeEmptyWithBOM()
 
127
  {
 
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));
 
132
  }
 
133
 
 
134
};
 
135
 
 
136
CPPUNIT_TEST_SUITE_REGISTRATION(TestString);