| 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 "tlist.h" |
| 27 | #include <cppunit/extensions/HelperMacros.h> |
| 28 | |
| 29 | using namespace std; |
| 30 | using namespace TagLib; |
| 31 | |
| 32 | class TestList : public CppUnit::TestFixture |
| 33 | { |
| 34 | CPPUNIT_TEST_SUITE(TestList); |
| 35 | CPPUNIT_TEST(testAppend); |
| 36 | CPPUNIT_TEST(testDetach); |
| 37 | CPPUNIT_TEST(bracedInit); |
| 38 | CPPUNIT_TEST(testSort); |
| 39 | CPPUNIT_TEST_SUITE_END(); |
| 40 | |
| 41 | public: |
| 42 | |
| 43 | void testAppend() |
| 44 | { |
| 45 | List<int> l1; |
| 46 | List<int> l2; |
| 47 | List<int> l3; |
| 48 | l1.append(2); |
| 49 | l2.append(3); |
| 50 | l2.append(4); |
| 51 | l1.append(l2); |
| 52 | l1.prepend(1); |
| 53 | l3.append(1); |
| 54 | l3.append(2); |
| 55 | l3.append(3); |
| 56 | l3.append(4); |
| 57 | CPPUNIT_ASSERT_EQUAL(4U, l1.size()); |
| 58 | CPPUNIT_ASSERT(l1 == l3); |
| 59 | } |
| 60 | |
| 61 | void testDetach() |
| 62 | { |
| 63 | { |
| 64 | List<int> l1; |
| 65 | l1.append(1); |
| 66 | l1.append(2); |
| 67 | l1.append(3); |
| 68 | l1.append(4); |
| 69 | |
| 70 | List<int> l2 = l1; |
| 71 | auto it = l2.find(3); |
| 72 | *it = 33; |
| 73 | CPPUNIT_ASSERT_EQUAL(3, l1[2]); |
| 74 | CPPUNIT_ASSERT_EQUAL(33, l2[2]); |
| 75 | } |
| 76 | { |
| 77 | List<int *> l1; |
| 78 | List<int *> l2 = l1; |
| 79 | CPPUNIT_ASSERT(!l1.autoDelete()); |
| 80 | CPPUNIT_ASSERT(!l2.autoDelete()); |
| 81 | l2.setAutoDelete(true); |
| 82 | CPPUNIT_ASSERT(!l1.autoDelete()); |
| 83 | CPPUNIT_ASSERT(l2.autoDelete()); |
| 84 | } |
| 85 | { |
| 86 | List<int> l1; |
| 87 | List<int> l2 = l1; |
| 88 | l1.insert(l1.begin(), 1); |
| 89 | CPPUNIT_ASSERT(!l1.isEmpty()); |
| 90 | CPPUNIT_ASSERT_EQUAL(1, l1.front()); |
| 91 | CPPUNIT_ASSERT(l2.isEmpty()); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | void bracedInit() |
| 96 | { |
| 97 | List<int> l1 { |
| 98 | 1, |
| 99 | 2, |
| 100 | 3 |
| 101 | }; |
| 102 | CPPUNIT_ASSERT_EQUAL(3, static_cast<int>(l1.size())); |
| 103 | CPPUNIT_ASSERT_EQUAL(1, l1[0]); |
| 104 | CPPUNIT_ASSERT_EQUAL(2, l1[1]); |
| 105 | CPPUNIT_ASSERT_EQUAL(3, l1[2]); |
| 106 | |
| 107 | List<int*> l2 { |
| 108 | new int(1), |
| 109 | new int(2), |
| 110 | new int(3) |
| 111 | }; |
| 112 | l2.setAutoDelete(true); |
| 113 | CPPUNIT_ASSERT_EQUAL(3, static_cast<int>(l2.size())); |
| 114 | CPPUNIT_ASSERT_EQUAL(1, *l2[0]); |
| 115 | CPPUNIT_ASSERT_EQUAL(2, *l2[1]); |
| 116 | CPPUNIT_ASSERT_EQUAL(3, *l2[2]); |
| 117 | |
| 118 | List<int> l3 = { |
| 119 | 1, |
| 120 | 2, |
| 121 | 3 |
| 122 | }; |
| 123 | CPPUNIT_ASSERT_EQUAL(3, static_cast<int>(l3.size())); |
| 124 | CPPUNIT_ASSERT_EQUAL(1, l3[0]); |
| 125 | CPPUNIT_ASSERT_EQUAL(2, l3[1]); |
| 126 | CPPUNIT_ASSERT_EQUAL(3, l3[2]); |
| 127 | |
| 128 | List<int*> l4 = { |
| 129 | new int(1), |
| 130 | new int(2), |
| 131 | new int(3) |
| 132 | }; |
| 133 | l4.setAutoDelete(true); |
| 134 | l4 = { |
| 135 | new int(4), |
| 136 | new int(5), |
| 137 | new int(6) |
| 138 | }; |
| 139 | CPPUNIT_ASSERT_EQUAL(3, static_cast<int>(l4.size())); |
| 140 | CPPUNIT_ASSERT_EQUAL(4, *l4[0]); |
| 141 | CPPUNIT_ASSERT_EQUAL(5, *l4[1]); |
| 142 | CPPUNIT_ASSERT_EQUAL(6, *l4[2]); |
| 143 | } |
| 144 | |
| 145 | void testSort() |
| 146 | { |
| 147 | List<int> list1 { |
| 148 | 3, |
| 149 | 2, |
| 150 | 1 |
| 151 | }; |
| 152 | list1.sort(); |
| 153 | CPPUNIT_ASSERT_EQUAL(list1[0], 1); |
| 154 | CPPUNIT_ASSERT_EQUAL(list1[1], 2); |
| 155 | CPPUNIT_ASSERT_EQUAL(list1[2], 3); |
| 156 | |
| 157 | List<int> list2 { |
| 158 | 1, |
| 159 | 2, |
| 160 | 3 |
| 161 | }; |
| 162 | list2.sort([](const auto &a, const auto &b) { return a > b; }); |
| 163 | CPPUNIT_ASSERT_EQUAL(list2[0], 3); |
| 164 | CPPUNIT_ASSERT_EQUAL(list2[1], 2); |
| 165 | CPPUNIT_ASSERT_EQUAL(list2[2], 1); |
| 166 | } |
| 167 | |
| 168 | }; |
| 169 | |
| 170 | CPPUNIT_TEST_SUITE_REGISTRATION(TestList); |