| 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 "tstring.h" |
| 27 | #include "tmap.h" |
| 28 | #include <cppunit/extensions/HelperMacros.h> |
| 29 | |
| 30 | using namespace std; |
| 31 | using namespace TagLib; |
| 32 | |
| 33 | class TestMap : public CppUnit::TestFixture |
| 34 | { |
| 35 | CPPUNIT_TEST_SUITE(TestMap); |
| 36 | CPPUNIT_TEST(testInsert); |
| 37 | CPPUNIT_TEST(testDetach); |
| 38 | CPPUNIT_TEST(testBracedInit); |
| 39 | CPPUNIT_TEST_SUITE_END(); |
| 40 | |
| 41 | public: |
| 42 | |
| 43 | void testInsert() |
| 44 | { |
| 45 | Map<String, int> m1; |
| 46 | m1.insert("foo", 3); |
| 47 | m1.insert("bar", 5); |
| 48 | CPPUNIT_ASSERT_EQUAL(2U, m1.size()); |
| 49 | CPPUNIT_ASSERT_EQUAL(3, m1["foo"]); |
| 50 | CPPUNIT_ASSERT_EQUAL(5, m1["bar"]); |
| 51 | m1.insert("foo", 7); |
| 52 | CPPUNIT_ASSERT_EQUAL(2U, m1.size()); |
| 53 | CPPUNIT_ASSERT_EQUAL(7, m1["foo"]); |
| 54 | CPPUNIT_ASSERT_EQUAL(5, m1["bar"]); |
| 55 | } |
| 56 | |
| 57 | void testDetach() |
| 58 | { |
| 59 | Map<String, int> m1; |
| 60 | m1.insert("alice", 5); |
| 61 | m1.insert("bob", 9); |
| 62 | m1.insert("carol", 11); |
| 63 | |
| 64 | Map<String, int> m2 = m1; |
| 65 | auto it = m2.find("bob"); |
| 66 | it->second = 99; |
| 67 | CPPUNIT_ASSERT_EQUAL(9, m1["bob"]); |
| 68 | CPPUNIT_ASSERT_EQUAL(99, m2["bob"]); |
| 69 | } |
| 70 | |
| 71 | void testBracedInit() |
| 72 | { |
| 73 | Map<String, int> m1 { |
| 74 | {"ONE", 1}, |
| 75 | {"TWO", 2}, |
| 76 | {"THREE", 3} |
| 77 | }; |
| 78 | CPPUNIT_ASSERT_EQUAL(3, static_cast<int>(m1.size())); |
| 79 | CPPUNIT_ASSERT(m1.contains("ONE") && m1["ONE"] == 1); |
| 80 | CPPUNIT_ASSERT(m1.contains("TWO") && m1["TWO"] == 2); |
| 81 | CPPUNIT_ASSERT(m1.contains("THREE") && m1["THREE"] == 3); |
| 82 | |
| 83 | Map<String, int> m2 = { |
| 84 | {"FOUR", 4}, |
| 85 | {"FIVE", 5}, |
| 86 | {"SIX", 6} |
| 87 | }; |
| 88 | CPPUNIT_ASSERT_EQUAL(3, static_cast<int>(m2.size())); |
| 89 | CPPUNIT_ASSERT(m2.contains("FOUR") && m2["FOUR"] == 4); |
| 90 | CPPUNIT_ASSERT(m2.contains("FIVE") && m2["FIVE"] == 5); |
| 91 | CPPUNIT_ASSERT(m2.contains("SIX") && m2["SIX"] == 6); |
| 92 | } |
| 93 | |
| 94 | }; |
| 95 | |
| 96 | CPPUNIT_TEST_SUITE_REGISTRATION(TestMap); |