diff --git a/tests/toolkit-test.cpp b/tests/toolkit-test.cpp new file mode 100644 index 00000000..ba49de25 --- /dev/null +++ b/tests/toolkit-test.cpp @@ -0,0 +1,230 @@ +/* Copyright (C) 2003 Scott Wheeler + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include + +#include +#include +#include +#include +#include + +#include + +using namespace TagLib; +using namespace std; + +void testString(); +void testByteVector(); +void testSynchData(); +void testList(); + +static int resultCount = 1; + +int main() +{ + testString(); + resultCount = 1; + testByteVector(); + resultCount = 1; + testSynchData(); + resultCount = 1; + testList(); + + return 0; +} + +void printResult(bool result) +{ + if(result) + cout << "(" << resultCount << ")\tpass" << endl; + else + cout << "(" << resultCount << ")\tFAIL" << endl; + + resultCount++; +} + +void testString() +{ + cout << "*** Testing TagLib::String ***" << endl; + + String s = "taglib string"; + ByteVector v = "taglib string"; + printResult(v == s.data(String::Latin1)); + + char str[] = "taglib string"; + printResult(strcmp(s.toCString(), str) == 0); + + String unicode("José Carlos", String::UTF8); + printResult(strcmp(unicode.toCString(), "José Carlos") == 0); + + String latin = "José Carlos"; + printResult(strcmp(latin.toCString(true), "José Carlos") == 0); + + String unicode2(unicode.to8Bit(true), String::UTF8); + printResult(unicode == unicode2); + + printResult(strcmp(String::number(0).toCString(), "0") == 0); + printResult(strcmp(String::number(12345678).toCString(), "12345678") == 0); + printResult(strcmp(String::number(-12345678).toCString(), "-12345678") == 0); + + String n = "123"; + printResult(n.toInt() == 123); + + n = "-123"; + printResult(n.toInt() == -123); + + printResult(String("0").toInt() == 0); + printResult(String("1").toInt() == 1); + + printResult(String(" foo ").stripWhiteSpace() == String("foo")); + printResult(String("foo ").stripWhiteSpace() == String("foo")); + printResult(String(" foo").stripWhiteSpace() == String("foo")); + + printResult(memcmp(String("foo").data(String::Latin1).data(), "foo", 3) == 0); + printResult(memcmp(String("f").data(String::Latin1).data(), "f", 1) == 0); +} + +void testConversion(unsigned int i, unsigned char a, unsigned char b, unsigned char c, unsigned char d) +{ + ByteVector v(4, 0); + + v[3] = a; + v[2] = b; + v[1] = c; + v[0] = d; + printResult(v.toUInt(false) == i); + + v[0] = a; + v[1] = b; + v[2] = c; + v[3] = d; + printResult(v.toUInt() == i); +} + + +void testByteVector() +{ + cout << "*** Testing TagLib::ByteVector ***" << endl; + ByteVector v("foobar"); + + printResult(v.find("ob") == 2); + printResult(v.find('b') == 3); + + ByteVector n(4, 0); + n[0] = 1; + printResult(n.toUInt(true) == 16777216); + printResult(n.toUInt(false) == 1); + printResult(ByteVector::fromUInt(16777216, true) == n); + printResult(ByteVector::fromUInt(1, false) == n); + + printResult(ByteVector::fromUInt(0xa0).toUInt() == 0xa0); + + testConversion(0x000000a0, 0x00, 0x00, 0x00, 0xa0); + testConversion(0xd50bf072, 0xd5, 0x0b, 0xf0, 0x72); + + ByteVector r0("**************"); + ByteVector r1("OggS**********"); + ByteVector r2("**********OggS"); + ByteVector r3("OggS******OggS"); + ByteVector r4("OggS*OggS*OggS"); + + printResult(r0.find("OggS") == -1); + printResult(r0.rfind("OggS") == -1); + printResult(r1.find("OggS") == r1.rfind("OggS")); + printResult(r2.find("OggS") == r2.rfind("OggS")); + printResult(r3.find("OggS") == 0); + printResult(r3.rfind("OggS") == 10); + printResult(r4.rfind("OggS") == 10); + printResult(r4.rfind("OggS", 12) == 5); + + printResult(ByteVector::fromLongLong(1).toLongLong() == 1); + printResult(ByteVector::fromLongLong(0).toLongLong() == 0); + printResult(ByteVector::fromLongLong(0xffffffffffffffffLL).toLongLong() == -1); + printResult(ByteVector::fromLongLong(0xfffffffffffffffeLL).toLongLong() == -2); + printResult(ByteVector::fromLongLong(1024).toLongLong() == 1024); + + ByteVector a1("foo"); + a1.append("bar"); + printResult(a1 == "foobar"); + + ByteVector a2("foo"); + a2.append("b"); + printResult(a2 == "foob"); + + ByteVector a3; + a3.append("b"); + printResult(a3 == "b"); + + ByteVector s1("foo"); + printResult(ByteVectorList::split(s1, " ").size() == 1); + + ByteVector s2("f"); + printResult(ByteVectorList::split(s2, " ").size() == 1); +} + +void testSynchData() +{ + cout << "*** Testing TagLib::ID3v2::SynchData ***" << endl; + + { // test 1 + char data[] = { 0, 0, 0, 127 }; + ByteVector v(data, 4); + + printResult(ID3v2::SynchData::toUInt(v) == 127); + printResult(ID3v2::SynchData::fromUInt(127) == v); + } + { // test 2 + char data[] = { 0, 0, 1, 0 }; + ByteVector v(data, 4); + + printResult(ID3v2::SynchData::toUInt(v) == 128); + printResult(ID3v2::SynchData::fromUInt(128) == v); + } + { // test 3 + char data[] = { 0, 0, 1, 1 }; + ByteVector v(data, 4); + + printResult(ID3v2::SynchData::toUInt(v) == 129); + printResult(ID3v2::SynchData::fromUInt(129) == v); + } +} + +void testList() +{ + cout << "*** Testing TagLib::List ***" << endl; + List l1; + List l2; + List l3; + l1.append(1); + l1.append(2); + l2.append(3); + l2.append(4); + l1.append(l2); + l3.append(1); + l3.append(2); + l3.append(3); + l3.append(4); + printResult(l1 == l3); +} diff --git a/toolkit/tbytevectorlist.cpp b/toolkit/tbytevectorlist.cpp new file mode 100644 index 00000000..3799f5e7 --- /dev/null +++ b/toolkit/tbytevectorlist.cpp @@ -0,0 +1,88 @@ +/*************************************************************************** + copyright : (C) 2002, 2003 by Scott Wheeler + email : wheeler@kde.org + ***************************************************************************/ + +/*************************************************************************** + * This library is free software; you can redistribute it and/or modify * + * it under the terms of the GNU Lesser General Public License version * + * 2.1 as published by the Free Software Foundation. * + * * + * This library is distributed in the hope that it will be useful, but * + * WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this library; if not, write to the Free Software * + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * + * USA * + ***************************************************************************/ + +#include "tbytevectorlist.h" + +using namespace TagLib; + +class ByteVectorListPrivate +{ + +}; + +//////////////////////////////////////////////////////////////////////////////// +// static members +//////////////////////////////////////////////////////////////////////////////// + +ByteVectorList ByteVectorList::split(const ByteVector &v, const ByteVector &pattern, + int byteAlign) +{ + ByteVectorList l; + + uint previousOffset = 0; + for(int offset = v.find(pattern, 0, byteAlign); + offset != -1; + offset = v.find(pattern, offset + pattern.size(), byteAlign)) + { + l.append(v.mid(previousOffset, offset - previousOffset)); + previousOffset = offset + pattern.size(); + } + + if(previousOffset < v.size()) + l.append(v.mid(previousOffset, v.size() - previousOffset)); + + return l; +} + +//////////////////////////////////////////////////////////////////////////////// +// public members +//////////////////////////////////////////////////////////////////////////////// + +ByteVectorList::ByteVectorList() : List() +{ + +} + +ByteVectorList::ByteVectorList(const ByteVectorList &l) : List(l) +{ + +} + +ByteVectorList::~ByteVectorList() +{ + +} + +ByteVector ByteVectorList::toByteVector(const ByteVector &separator) const +{ + ByteVector v; + + ConstIterator it = begin(); + + while(it != end()) { + v.append(*it); + it++; + if(it != end()) + v.append(separator); + } + + return v; +}