File: test_map.cpp

package info (click to toggle)
taglib 2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,192 kB
  • sloc: cpp: 44,736; ansic: 137; makefile: 28
file content (96 lines) | stat: -rw-r--r-- 3,450 bytes parent folder | download | duplicates (3)
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
/***************************************************************************
    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 "tstring.h"
#include "tmap.h"
#include <cppunit/extensions/HelperMacros.h>

using namespace std;
using namespace TagLib;

class TestMap : public CppUnit::TestFixture
{
  CPPUNIT_TEST_SUITE(TestMap);
  CPPUNIT_TEST(testInsert);
  CPPUNIT_TEST(testDetach);
  CPPUNIT_TEST(testBracedInit);
  CPPUNIT_TEST_SUITE_END();

public:

  void testInsert()
  {
    Map<String, int> m1;
    m1.insert("foo", 3);
    m1.insert("bar", 5);
    CPPUNIT_ASSERT_EQUAL(2U, m1.size());
    CPPUNIT_ASSERT_EQUAL(3, m1["foo"]);
    CPPUNIT_ASSERT_EQUAL(5, m1["bar"]);
    m1.insert("foo", 7);
    CPPUNIT_ASSERT_EQUAL(2U, m1.size());
    CPPUNIT_ASSERT_EQUAL(7, m1["foo"]);
    CPPUNIT_ASSERT_EQUAL(5, m1["bar"]);
  }

  void testDetach()
  {
    Map<String, int> m1;
    m1.insert("alice", 5);
    m1.insert("bob", 9);
    m1.insert("carol", 11);

    Map<String, int> m2 = m1;
    auto it = m2.find("bob");
    it->second = 99;
    CPPUNIT_ASSERT_EQUAL(9,  m1["bob"]);
    CPPUNIT_ASSERT_EQUAL(99, m2["bob"]);
  }

  void testBracedInit()
  {
    Map<String, int> m1 {
      {"ONE", 1},
      {"TWO", 2},
      {"THREE", 3}
    };
    CPPUNIT_ASSERT_EQUAL(3, static_cast<int>(m1.size()));
    CPPUNIT_ASSERT(m1.contains("ONE") && m1["ONE"] == 1);
    CPPUNIT_ASSERT(m1.contains("TWO") && m1["TWO"] == 2);
    CPPUNIT_ASSERT(m1.contains("THREE") && m1["THREE"] == 3);

    Map<String, int> m2 = {
      {"FOUR", 4},
      {"FIVE", 5},
      {"SIX", 6}
    };
    CPPUNIT_ASSERT_EQUAL(3, static_cast<int>(m2.size()));
    CPPUNIT_ASSERT(m2.contains("FOUR") && m2["FOUR"] == 4);
    CPPUNIT_ASSERT(m2.contains("FIVE") && m2["FIVE"] == 5);
    CPPUNIT_ASSERT(m2.contains("SIX") && m2["SIX"] == 6);
  }

};

CPPUNIT_TEST_SUITE_REGISTRATION(TestMap);