Jamoma API  0.6.0.a19
TTDictionary.test.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup foundationLibrary
4  *
5  * @brief #TTDictionaryTest is an class used for unit tests of the #TTDictionary class.
6  *
7  * @details
8  *
9  * @author Théo de la Hogue, Tim Place
10  *
11  * @copyright Copyright © 2013, Théo de la Hogue, Tim Place @n
12  * This code is licensed under the terms of the "New BSD License" @n
13  * http://creativecommons.org/licenses/BSD/
14  */
15 
16 #include "TTDictionary.test.h"
17 
18 #define thisTTClass TTDictionaryTest
19 #define thisTTClassName "dictionary.test"
20 #define thisTTClassTags "test, foundation"
21 
22 
24 {;}
25 
26 
27 TTDictionaryTest::~TTDictionaryTest()
28 {;}
29 
30 
31 void TTDictionaryTestBasic(int& errorCount, int& testAssertionCount)
32 {
33  TTTestLog("\n");
34  TTTestLog("Testing dictionary basic operations");
35 
36  TTTestLog("\n");
37  TTTestLog("Testing dictionary creation");
38 
39  // creation using a random name
40  TTDictionary d1;
41 
42  TTTestAssertion("TTDictionary random name : Test fails if the dictionary have no name",
43  d1.name() != kTTSymEmpty,
44  testAssertionCount,
45  errorCount);
46 
47  // creation using a specific string name
48  TTDictionary d2("dictionary2");
49 
50  TTTestAssertion("TTDictionary string name : Test fails if the dictionary name is not \"dictionary2\"",
51  d2.name() == TTSymbol("dictionary2"),
52  testAssertionCount,
53  errorCount);
54 
55  // creation using a specific symbol name
56  TTDictionary d3(kTTSym_symbol);
57 
58  TTTestAssertion("TTDictionary symbol name : Test fails if the dictionary name is not \"symbol\"",
59  d3.name() == kTTSym_symbol,
60  testAssertionCount,
61  errorCount);
62 
63 
64 
65  TTTestLog("\n");
66  TTTestLog("Testing dictionary schema");
67 
68  TTTestAssertion("TTDictionary schema : dictionary schema should default to 'none' ",
69  d1.getSchema() == "none",
70  testAssertionCount,
71  errorCount);
72 
73  d1.setSchema(TTSymbol("aSchemaName"));
74 
75  TTTestAssertion("TTDictionary schema : Test fails if the dictionary schema is not \"aSchemaName\"",
76  d1.getSchema() == TTSymbol("aSchemaName"),
77  testAssertionCount,
78  errorCount);
79 
80 
81 
82  TTTestLog("\n");
83  TTTestLog("Testing dictionary value");
84 
85  TTValue v;
86  d1.getValue(v);
87 
88  TTTestAssertion("TTDictionary value : Test fails if the dictionary value is not empty",
89  v == TTValue(),
90  testAssertionCount,
91  errorCount);
92 
93  d1.setValue(1);
94  d1.getValue(v);
95 
96  TTTestAssertion("TTDictionary value : Test fails if the dictionary value is not kTTVal1",
97  v == 1,
98  testAssertionCount,
99  errorCount);
100 
101 
102 
103  TTTestLog("\n");
104  TTTestLog("Testing dictionary keys");
105 
106  d1.append(kTTSym_gain, 1);
107  TTErr err = d1.lookup(kTTSym_gain, v);
108 
109  TTTestAssertion("TTDictionary append key : Test fails if the dictionary key \"gain\" doesn't exist or its value is not kTTVal1",
110  err == kTTErrNone &&
111  v == 1,
112  testAssertionCount,
113  errorCount);
114 
115  d1.getKeys(v);
116  TTSymbol k1 = v[2];
117  TTSymbol k2 = v[1];
118  TTSymbol k3 = v[0];
119 
120  TTTestAssertion("TTDictionary get keys : Test fails if the dictionary keys are not \"schema\", \"value\" and \"gain\" or the size is not 3",
121  k1 == kTTSym_schema &&
122  k2 == kTTSym_value &&
123  k3 == kTTSym_gain &&
124  d1.size() == 3,
125  testAssertionCount,
126  errorCount);
127 
128  d1.remove(kTTSym_gain);
129  d1.getKeys(v);
130  k1 = v[1];
131  k2 = v[0];
132 
133  TTTestAssertion("TTDictionary remove key : Test fails if the dictionary keys are not \"schema\" and \"value\" or the size is not 2",
134  k1 == kTTSym_schema &&
135  k2 == kTTSym_value &&
136  d1.size() == 2,
137  testAssertionCount,
138  errorCount);
139 
140  d1.clear();
141 
142  TTTestAssertion("TTDictionary clear keys : Test fails if the dictionary keys are not empty or the size is not 0",
143  d1.empty() &&
144  d1.size() == 0,
145  testAssertionCount,
146  errorCount);
147 }
148 
149 void TTDictionaryTestValue(int& errorCount, int& testAssertionCount)
150 {
151  TTTestLog("\n");
152  TTTestLog("Testing dictionary inside value");
153 
154  TTDictionary d1("dictionary1");
155  TTValue v = d1;
156 
157  TTTestAssertion("TTDictionary value assignement : Test fails if the value type is not dictionary",
158  v[0].type() == kTypeDictionary,
159  testAssertionCount,
160  errorCount);
161 
162  TTDictionary d2 = v[0];
163 
164  TTTestAssertion("TTDictionary value assignement : Test fails if two dictionaries haven't the same name",
165  d2.name() == d1.name(),
166  testAssertionCount,
167  errorCount);
168 }
169 
170 TTErr TTDictionaryTest::test(TTValue& returnedTestInfo)
171 {
172  int errorCount = 0;
173  int testAssertionCount = 0;
174 
175  TTDictionaryTestBasic(errorCount, testAssertionCount);
176 
177  TTDictionaryTestValue(errorCount, testAssertionCount);
178 
179  return TTTestFinish(testAssertionCount, errorCount, returnedTestInfo);
180 }
TTPtrSizedInt size()
Return the number of keys in the hash table.
Definition: TTDictionary.h:259
TTErr append(const TTSymbol aKey, const TTValue aValue)
Insert an item into the hash table.
Definition: TTDictionary.h:212
TTSymbol name() const
Return the name associated with the dictionary.
Definition: TTDictionary.h:131
TTErr clear()
Remove all items from the hash table.
Definition: TTDictionary.h:240
Dictionary type.
Definition: TTBase.h:288
TTErr getKeys(TTValue &aSetOfKeys)
Get an array of all of the keys for the hash table.
Definition: TTDictionary.h:250
TTErr getValue(T &aReturnedValue) const
Get the dictionary's primary value.
Definition: TTDictionary.h:202
A type that represents the key as a C-String and the value as a pointer to the matching TTSymbol obje...
Definition: TTDictionary.h:47
TTDictionaryTest is an class used for unit tests of the TTDictionary class.
The TTSymbol class is used to represent a string and efficiently pass and compare that string...
Definition: TTSymbol.h:26
const TTSymbol getSchema() const
TODO: Add documentation.
Definition: TTDictionary.h:181
TTBoolean empty()
Return true if the hash has nothing stored in it.
Definition: TTDictionary.h:268
TTErr setSchema(const TTSymbol aSchemaName)
TODO: Add documentation schemaName TODO: Add documentation.
Definition: TTDictionary.h:172
TTErr setValue(const TTValue aNewValue)
TODO: Add documentation.
Definition: TTDictionary.h:191
TTErr
Jamoma Error Codes Enumeration of error codes that might be returned by any of the TTBlue functions a...
Definition: TTBase.h:342
TTErr lookup(const TTSymbol aKey, TTValue &aValue) const
Find the value for the given key.
Definition: TTDictionary.h:221
No Error.
Definition: TTBase.h:343
TT_OBJECT_CONSTRUCTOR
Constructor macro.
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34
TTErr remove(const TTSymbol aKey)
Remove an item from the hash table.
Definition: TTDictionary.h:231