| 1 | /*************************************************************************** |
| 2 | copyright : (C) 2007 by Lukas Lalinsky |
| 3 | email : [email protected] |
| 4 | ***************************************************************************/ |
| 5 | |
| 6 | /*************************************************************************** |
| 7 | * This library is free software; you can redistribute it and/or modify * |
| 8 | * it under the terms of the GNU Lesser General Public License version * |
| 9 | * 2.1 as published by the Free Software Foundation. * |
| 10 | * * |
| 11 | * This library is distributed in the hope that it will be useful, but * |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 14 | * Lesser General Public License for more details. * |
| 15 | * * |
| 16 | * You should have received a copy of the GNU Lesser General Public * |
| 17 | * License along with this library; if not, write to the Free Software * |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA * |
| 19 | * 02110-1301 USA * |
| 20 | * * |
| 21 | * Alternatively, this file is available under the Mozilla Public * |
| 22 | * License Version 1.1. You may obtain a copy of the License at * |
| 23 | * http://www.mozilla.org/MPL/ * |
| 24 | ***************************************************************************/ |
| 25 | |
| 26 | #include "id3v2synchdata.h" |
| 27 | #include <cppunit/extensions/HelperMacros.h> |
| 28 | |
| 29 | using namespace std; |
| 30 | using namespace TagLib; |
| 31 | |
| 32 | class TestID3v2SynchData : public CppUnit::TestFixture |
| 33 | { |
| 34 | CPPUNIT_TEST_SUITE(TestID3v2SynchData); |
| 35 | CPPUNIT_TEST(test1); |
| 36 | CPPUNIT_TEST(test2); |
| 37 | CPPUNIT_TEST(test3); |
| 38 | CPPUNIT_TEST(testToUIntBroken); |
| 39 | CPPUNIT_TEST(testToUIntBrokenAndTooLarge); |
| 40 | CPPUNIT_TEST(testDecode1); |
| 41 | CPPUNIT_TEST(testDecode2); |
| 42 | CPPUNIT_TEST(testDecode3); |
| 43 | CPPUNIT_TEST(testDecode4); |
| 44 | CPPUNIT_TEST_SUITE_END(); |
| 45 | |
| 46 | public: |
| 47 | |
| 48 | void test1() |
| 49 | { |
| 50 | char data[] = { 0, 0, 0, 127 }; |
| 51 | ByteVector v(data, 4); |
| 52 | |
| 53 | CPPUNIT_ASSERT_EQUAL(ID3v2::SynchData::toUInt(v), static_cast<unsigned int>(127)); |
| 54 | CPPUNIT_ASSERT_EQUAL(ID3v2::SynchData::fromUInt(127), v); |
| 55 | } |
| 56 | |
| 57 | void test2() |
| 58 | { |
| 59 | char data[] = { 0, 0, 1, 0 }; |
| 60 | ByteVector v(data, 4); |
| 61 | |
| 62 | CPPUNIT_ASSERT_EQUAL(ID3v2::SynchData::toUInt(v), static_cast<unsigned int>(128)); |
| 63 | CPPUNIT_ASSERT_EQUAL(ID3v2::SynchData::fromUInt(128), v); |
| 64 | } |
| 65 | |
| 66 | void test3() |
| 67 | { |
| 68 | char data[] = { 0, 0, 1, 1 }; |
| 69 | ByteVector v(data, 4); |
| 70 | |
| 71 | CPPUNIT_ASSERT_EQUAL(ID3v2::SynchData::toUInt(v), static_cast<unsigned int>(129)); |
| 72 | CPPUNIT_ASSERT_EQUAL(ID3v2::SynchData::fromUInt(129), v); |
| 73 | } |
| 74 | |
| 75 | void testToUIntBroken() |
| 76 | { |
| 77 | char data[] = { 0, 0, 0, static_cast<char>(-1) }; |
| 78 | char data2[] = { 0, 0, static_cast<char>(-1), static_cast<char>(-1) }; |
| 79 | |
| 80 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(255), ID3v2::SynchData::toUInt(ByteVector(data, 4))); |
| 81 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(65535), ID3v2::SynchData::toUInt(ByteVector(data2, 4))); |
| 82 | } |
| 83 | |
| 84 | void testToUIntBrokenAndTooLarge() |
| 85 | { |
| 86 | char data[] = { 0, 0, 0, static_cast<char>(-1), 0 }; |
| 87 | ByteVector v(data, 5); |
| 88 | |
| 89 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(255), ID3v2::SynchData::toUInt(v)); |
| 90 | } |
| 91 | |
| 92 | void testDecode1() |
| 93 | { |
| 94 | ByteVector a("\xff\x00\x00", 3); |
| 95 | a = ID3v2::SynchData::decode(a); |
| 96 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(2), a.size()); |
| 97 | CPPUNIT_ASSERT_EQUAL(ByteVector("\xff\x00", 2), a); |
| 98 | } |
| 99 | |
| 100 | void testDecode2() |
| 101 | { |
| 102 | ByteVector a("\xff\x44", 2); |
| 103 | a = ID3v2::SynchData::decode(a); |
| 104 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(2), a.size()); |
| 105 | CPPUNIT_ASSERT_EQUAL(ByteVector("\xff\x44", 2), a); |
| 106 | } |
| 107 | |
| 108 | void testDecode3() |
| 109 | { |
| 110 | ByteVector a("\xff\xff\x00", 3); |
| 111 | a = ID3v2::SynchData::decode(a); |
| 112 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(2), a.size()); |
| 113 | CPPUNIT_ASSERT_EQUAL(ByteVector("\xff\xff", 2), a); |
| 114 | } |
| 115 | |
| 116 | void testDecode4() |
| 117 | { |
| 118 | ByteVector a("\xff\xff\xff", 3); |
| 119 | a = ID3v2::SynchData::decode(a); |
| 120 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(3), a.size()); |
| 121 | CPPUNIT_ASSERT_EQUAL(ByteVector("\xff\xff\xff", 3), a); |
| 122 | } |
| 123 | |
| 124 | }; |
| 125 | |
| 126 | CPPUNIT_TEST_SUITE_REGISTRATION(TestID3v2SynchData); |