aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_synchdata.cpp
blob: 08d650794582792a301a5450c79ce937296a1a7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/***************************************************************************
    copyright           : (C) 2007 by Lukas Lalinsky
    email               : lukas@oxygene.sk
 ***************************************************************************/

/***************************************************************************
 *   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., 51 Franklin Street, Fifth Floor, Boston, MA         *
 *   02110-1301  USA                                                       *
 *                                                                         *
 *   Alternatively, this file is available under the Mozilla Public        *
 *   License Version 1.1.  You may obtain a copy of the License at         *
 *   http://www.mozilla.org/MPL/                                           *
 ***************************************************************************/


#include <id3v2synchdata.h>
#include <cppunit/extensions/HelperMacros.h>

using namespace std;
using namespace TagLib;

class TestID3v2SynchData : public CppUnit::TestFixture
{
  CPPUNIT_TEST_SUITE(TestID3v2SynchData);
  CPPUNIT_TEST(test1);
  CPPUNIT_TEST(test2);
  CPPUNIT_TEST(test3);
  CPPUNIT_TEST(testToUIntBroken);
  CPPUNIT_TEST(testToUIntBrokenAndTooLarge);
  CPPUNIT_TEST(testDecode1);
  CPPUNIT_TEST(testDecode2);
  CPPUNIT_TEST(testDecode3);
  CPPUNIT_TEST(testDecode4);
  CPPUNIT_TEST_SUITE_END();

public:

  void test1()
  {
    char data[] = { 0, 0, 0, 127 };
    ByteVector v(data, 4);

    CPPUNIT_ASSERT_EQUAL(ID3v2::SynchData::toUInt(v), (unsigned int)127);
    CPPUNIT_ASSERT_EQUAL(ID3v2::SynchData::fromUInt(127), v);
  }

  void test2()
  {
    char data[] = { 0, 0, 1, 0 };
    ByteVector v(data, 4);

    CPPUNIT_ASSERT_EQUAL(ID3v2::SynchData::toUInt(v), (unsigned int)128);
    CPPUNIT_ASSERT_EQUAL(ID3v2::SynchData::fromUInt(128), v);
  }

  void test3()
  {
    char data[] = { 0, 0, 1, 1 };
    ByteVector v(data, 4);

    CPPUNIT_ASSERT_EQUAL(ID3v2::SynchData::toUInt(v), (unsigned int)129);
    CPPUNIT_ASSERT_EQUAL(ID3v2::SynchData::fromUInt(129), v);
  }

  void testToUIntBroken()
  {
    char data[] = { 0, 0, 0, (char)-1 };
    char data2[] = { 0, 0, (char)-1, (char)-1 };

    CPPUNIT_ASSERT_EQUAL((unsigned int)255, ID3v2::SynchData::toUInt(ByteVector(data, 4)));
    CPPUNIT_ASSERT_EQUAL((unsigned int)65535, ID3v2::SynchData::toUInt(ByteVector(data2, 4)));
  }

  void testToUIntBrokenAndTooLarge()
  {
    char data[] = { 0, 0, 0, (char)-1, 0 };
    ByteVector v(data, 5);

    CPPUNIT_ASSERT_EQUAL((unsigned int)255, ID3v2::SynchData::toUInt(v));
  }

  void testDecode1()
  {
    ByteVector a("\xff\x00\x00", 3);
    a = ID3v2::SynchData::decode(a);
    CPPUNIT_ASSERT_EQUAL((unsigned int)2, a.size());
    CPPUNIT_ASSERT_EQUAL(ByteVector("\xff\x00", 2), a);
  }

  void testDecode2()
  {
    ByteVector a("\xff\x44", 2);
    a = ID3v2::SynchData::decode(a);
    CPPUNIT_ASSERT_EQUAL((unsigned int)2, a.size());
    CPPUNIT_ASSERT_EQUAL(ByteVector("\xff\x44", 2), a);
  }

  void testDecode3()
  {
    ByteVector a("\xff\xff\x00", 3);
    a = ID3v2::SynchData::decode(a);
    CPPUNIT_ASSERT_EQUAL((unsigned int)2, a.size());
    CPPUNIT_ASSERT_EQUAL(ByteVector("\xff\xff", 2), a);
  }

  void testDecode4()
  {
    ByteVector a("\xff\xff\xff", 3);
    a = ID3v2::SynchData::decode(a);
    CPPUNIT_ASSERT_EQUAL((unsigned int)3, a.size());
    CPPUNIT_ASSERT_EQUAL(ByteVector("\xff\xff\xff", 3), a);
  }

};

CPPUNIT_TEST_SUITE_REGISTRATION(TestID3v2SynchData);