1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /***************************************************************************
3  *            audiocacheidmanagertest.cc
4  *
5  *  Thu Jan  7 15:42:31 CET 2016
6  *  Copyright 2016 Bent Bisballe Nyeng
7  *  deva@aasimon.org
8  ****************************************************************************/
9 
10 /*
11  *  This file is part of DrumGizmo.
12  *
13  *  DrumGizmo is free software; you can redistribute it and/or modify
14  *  it under the terms of the GNU Lesser General Public License as published by
15  *  the Free Software Foundation; either version 3 of the License, or
16  *  (at your option) any later version.
17  *
18  *  DrumGizmo is distributed in the hope that it will be useful,
19  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  *  GNU Lesser General Public License for more details.
22  *
23  *  You should have received a copy of the GNU Lesser General Public License
24  *  along with DrumGizmo; if not, write to the Free Software
25  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
26  */
27 #include <uunit.h>
28 
29 #include <audiocacheidmanager.h>
30 
31 class TestableAudioCacheIDManager
32 	: public AudioCacheIDManager
33 {
34 public:
getAvailableIDs()35 	int getAvailableIDs()
36 	{
37 		return available_ids.size();
38 	}
39 };
40 
41 class AudioCacheIDManagerTest
42 	: public uUnit
43 {
44 public:
AudioCacheIDManagerTest()45 	AudioCacheIDManagerTest()
46 	{
47 		uUNIT_TEST(AudioCacheIDManagerTest::registerReleaseTest);
48 	}
49 
registerReleaseTest()50 	void registerReleaseTest()
51 	{
52 		TestableAudioCacheIDManager manager;
53 		manager.init(2);
54 
55 		cache_t c1; c1.afile = (AudioCacheFile*)1;
56 		auto id1 = manager.registerID(c1);
57 		uUNIT_ASSERT(id1 != CACHE_DUMMYID);
58 		uUNIT_ASSERT(id1 != CACHE_NOID);
59 		uUNIT_ASSERT_EQUAL(1, manager.getAvailableIDs());
60 
61 		cache_t c2; c2.afile = (AudioCacheFile*)2;
62 		auto id2 = manager.registerID(c2);
63 		uUNIT_ASSERT(id2 != CACHE_DUMMYID);
64 		uUNIT_ASSERT(id2 != CACHE_NOID);
65 		uUNIT_ASSERT_EQUAL(0, manager.getAvailableIDs());
66 
67 		cache_t c3; c3.afile = (AudioCacheFile*)3;
68 		auto id3 = manager.registerID(c3);
69 		uUNIT_ASSERT(id3 == CACHE_DUMMYID);
70 		uUNIT_ASSERT_EQUAL(0, manager.getAvailableIDs());
71 
72 		cache_t& tc1 = manager.getCache(id1);
73 		uUNIT_ASSERT_EQUAL(c1.afile, tc1.afile);
74 
75 		cache_t& tc2 = manager.getCache(id2);
76 		uUNIT_ASSERT_EQUAL(c2.afile, tc2.afile);
77 
78 		manager.releaseID(id1);
79 		uUNIT_ASSERT_EQUAL(1, manager.getAvailableIDs());
80 
81 		cache_t c4; c4.afile = (AudioCacheFile*)4;
82 		auto id4 = manager.registerID(c4);
83 		uUNIT_ASSERT(id4 != CACHE_DUMMYID);
84 		uUNIT_ASSERT(id4 != CACHE_NOID);
85 		uUNIT_ASSERT_EQUAL(0, manager.getAvailableIDs());
86 
87 		cache_t& tc4 = manager.getCache(id4);
88 		uUNIT_ASSERT_EQUAL(c4.afile, tc4.afile);
89 
90 		manager.releaseID(id2);
91 		uUNIT_ASSERT_EQUAL(1, manager.getAvailableIDs());
92 
93 		manager.releaseID(id4);
94 		uUNIT_ASSERT_EQUAL(2, manager.getAvailableIDs());
95 	}
96 };
97 
98 // Registers the fixture into the 'registry'
99 static AudioCacheIDManagerTest test;
100