File indexing completed on 2024-04-21 04:43:24

0001 /*
0002     SPDX-FileCopyrightText: 2018 David Rosca <nowrep@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include "mapstest.h"
0008 
0009 #include "maps.h"
0010 #include "module.h"
0011 
0012 #include <QtTest/QSignalSpy>
0013 #include <QtTest/QTest>
0014 
0015 using namespace PulseAudioQt;
0016 
0017 void MapsTest::initTestCase()
0018 {
0019 }
0020 
0021 void MapsTest::cleanupTestCase()
0022 {
0023 }
0024 
0025 static pa_module_info *module_info_new()
0026 {
0027     pa_module_info *info = (pa_module_info *)calloc(1, sizeof(pa_module_info));
0028     info->name = "name";
0029     info->argument = "arg";
0030     info->n_used = PA_INVALID_INDEX;
0031     info->proplist = pa_proplist_new();
0032     return info;
0033 }
0034 
0035 static void module_info_free(pa_module_info *info)
0036 {
0037     pa_proplist_free(info->proplist);
0038     free(info);
0039 }
0040 
0041 void MapsTest::basicTest()
0042 {
0043     QObject p;
0044     ModuleMap map;
0045 
0046     pa_module_info *info = module_info_new();
0047     info->index = 10;
0048 
0049     QCOMPARE(map.count(), 0);
0050 
0051     map.updateEntry(info, &p);
0052     QCOMPARE(map.count(), 1);
0053     map.updateEntry(info, &p);
0054     QCOMPARE(map.count(), 1);
0055 
0056     QObject *index10 = map.objectAt(0);
0057     QVERIFY(index10);
0058 
0059     QCOMPARE(map.indexOfObject(index10), 0);
0060 
0061     // Add 0 (10, 0)
0062     info->index = 0;
0063     map.updateEntry(info, &p);
0064     QCOMPARE(map.count(), 2);
0065     QCOMPARE(map.objectAt(0), index10);
0066 
0067     QObject *index0 = map.objectAt(1);
0068     QVERIFY(index0);
0069 
0070     QCOMPARE(map.indexOfObject(index0), 1);
0071     QCOMPARE(map.indexOfObject(index10), 0);
0072 
0073     // Add 4, 15 (10, 0, 4, 15)
0074     info->index = 4;
0075     map.updateEntry(info, &p);
0076     info->index = 15;
0077     map.updateEntry(info, &p);
0078 
0079     QCOMPARE(map.count(), 4);
0080     QCOMPARE(map.objectAt(0), index10);
0081     QCOMPARE(map.objectAt(1), index0);
0082 
0083     QObject *index4 = map.objectAt(2);
0084     QVERIFY(index4);
0085     QObject *index15 = map.objectAt(3);
0086     QVERIFY(index15);
0087 
0088     QCOMPARE(map.indexOfObject(index10), 0);
0089     QCOMPARE(map.indexOfObject(index0), 1);
0090     QCOMPARE(map.indexOfObject(index4), 2);
0091     QCOMPARE(map.indexOfObject(index15), 3);
0092 
0093     // Remove 4 (10, 0, 15)
0094     map.removeEntry(4);
0095     QCOMPARE(map.count(), 3);
0096     QCOMPARE(map.objectAt(0), index10);
0097     QCOMPARE(map.objectAt(1), index0);
0098     QCOMPARE(map.objectAt(2), index15);
0099 
0100     // Remove 0 (10, 15)
0101     map.removeEntry(0);
0102     QCOMPARE(map.count(), 2);
0103     QCOMPARE(map.objectAt(0), index10);
0104     QCOMPARE(map.objectAt(1), index15);
0105 
0106     // Remove 15 (10)
0107     map.removeEntry(15);
0108     QCOMPARE(map.count(), 1);
0109     QCOMPARE(map.objectAt(0), index10);
0110 
0111     // Remove last ()
0112     map.removeEntry(10);
0113     QCOMPARE(map.count(), 0);
0114 
0115     module_info_free(info);
0116 }
0117 
0118 void MapsTest::pendingRemovalsTest()
0119 {
0120     QObject p;
0121     ModuleMap map;
0122 
0123     pa_module_info *info = module_info_new();
0124     info->index = 10;
0125 
0126     QCOMPARE(map.count(), 0);
0127 
0128     // 10 is not in map, should set pending removal
0129     map.removeEntry(10);
0130     // 10 is in pending removals, this should eat it and not add it into map
0131     map.updateEntry(info, &p);
0132     QCOMPARE(map.count(), 0);
0133 
0134     // No more pending removals, this should add it
0135     map.updateEntry(info, &p);
0136     QCOMPARE(map.count(), 1);
0137 
0138     module_info_free(info);
0139 }
0140 
0141 QTEST_MAIN(MapsTest)