source: cpp/frams/_demos/simil_unittests.cpp @ 635

Last change on this file since 635 was 351, checked in by Maciej Komosinski, 10 years ago

Renamed simil_match_test.cpp -> simil_unittests.cpp

File size: 12.5 KB
RevLine 
[349]1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
2// Copyright (C) 1999-2015  Maciej Komosinski and Szymon Ulatowski.
3// See LICENSE.txt for details.
4
5#include "frams/model/similarity/simil_match.h"
6
7/** Runs unit tests of classes used in computation of similarity.
8 */
9#include <stdio.h>
10#include <assert.h>
11
12/** Prints out (standard output) the ERROR message.
13 */
14void PrintErrorMessage()
15{
16    printf("There were ERRORS during tests!\n");
17}
18
19/** Prints out (standard output) the OK message.
20 */
21void PrintPassedMessage()
22{
23    printf("Tests passed OK!\n");
24}
25
26/** Test construction of the SimilMatching objects and whether they are empty at
27    the beginning.
28        @return 0 if OK. Errors break execution (assert())
29 */
30int TestConstruction()
31{
32    const int nSize1 = 10;
33    const int nSize2 = 200;
34
35    // check symmetric matching
36    SimilMatching match1(nSize1, nSize1);
37
38    // test if all elements are unmatched yet
39    int i, j;
40    for (j = 0; j < 2; j++)
41    {
42        for (i = 0; i < nSize1; i++)
43        {
44            assert(match1.IsMatched(j, i) == false);
45        }
46    }
47
48    // test if all are unmatched - new method
49    assert(match1.IsEmpty() == true);
50
51    // check assymetric matching
52    SimilMatching match2(nSize1, nSize2);
53
54    // check if all elements are unmatched yet in object 0
55    for (i = 0; i < nSize1; i++)
56    {
57        assert(match2.IsMatched(0, i) == false);
58    }
59    // check if all elements are unmatched yet in object 1
60    for (i = 0; i < nSize2; i++)
61    {
62        assert(match2.IsMatched(1, i) == false);
63    }
64
65    // test if all are unmatched - new method
66    assert(match2.IsEmpty() == true);
67
68    return 0;
69}
70
71/** Tests if sizes of matching are appropriate.
72        @return 0 if OK. Errors break execution (assert()).
73 */
74int TestSizes()
75{
76    const int nSizeMax = 100;
77    SimilMatching *pMatching = NULL;
78
79    // construct objects of different size and check their sizes
80    int i, j;
81    for (i = 1; i < nSizeMax; i++)
82    {
83        for (j = 1; j < nSizeMax; j++)
84        {
85
86            // create a matching of size (i,j)
87            pMatching = new SimilMatching(i, j);
88            assert(pMatching != NULL);
89
90            // check size of both objects
91            assert(pMatching->GetObjectSize(0) == i);
92            assert(pMatching->GetObjectSize(1) == j);
93
94            // delete disused object
95            delete pMatching;
96        }
97    }
98    return 0;
99}
100
101/** Tests the copying constructor of 4 different, deterministic matchings:
102        empty, straight, reverse, sparse.
103        Also tests the method Empty().
104        @return 0 if OK. Errors break execution (assert())
105 */
106int TestCopyConstructor()
107{
108    // define the size used for creating matchings
109    const int iSize = 1000;
110    int iObj; // a counter of objects in matchings
111
112    {
113        // 1st test: create an empty matching, copy it, and check the copy
114        SimilMatching Source(iSize, iSize);
115        // check if it is empty
116        assert(Source.IsEmpty() == true);
117        // check sizes of objects
118        assert(Source.GetObjectSize(0) == iSize);
119        assert(Source.GetObjectSize(1) == iSize);
120        // create a copy of this matching
121        SimilMatching Dest(Source);
122        // check the copy:
123        // - sizes of matched object
124        assert(Dest.GetObjectSize(0) == Source.GetObjectSize(0));
125        assert(Dest.GetObjectSize(1) == Source.GetObjectSize(1));
126        // - the copy should be empty, too
127        assert(Dest.IsEmpty() == true);
128        // make it empty once again
129        Dest.Empty();
130        // and check once more
131        assert(Dest.IsEmpty() == true);
132    }
133
134    {
135        // 2nd test: create a straight matching (i, i), copy it, and check the copy
136        SimilMatching Source(iSize, iSize);
137        // check if it is empty
138        assert(Source.IsEmpty() == true);
139        // check sizes of objects
140        assert(Source.GetObjectSize(0) == iSize);
141        assert(Source.GetObjectSize(1) == iSize);
142        // match objects (iObj, iObj)
143        for (iObj = 0; iObj < iSize; iObj++)
144        {
145            Source.Match(0, iObj, 1, iObj);
146        }
147        // check if the matching is full
148        assert(Source.IsFull() == true);
149        // now create a copy of the matching
150        SimilMatching Dest(Source);
151        // check the copy
152        // - sizes of matched object
153        assert(Dest.GetObjectSize(0) == Source.GetObjectSize(0));
154        assert(Dest.GetObjectSize(1) == Source.GetObjectSize(1));
155        // - the copy should be full, too
156        assert(Dest.IsFull() == true);
157        // - the copy should have exactly the same assignments in matching
158        for (iObj = 0; iObj < iSize; iObj++)
159        {
160            // all object should be matched!
161            // check both directions: 0 -> 1
162            assert(Dest.IsMatched(0, iObj) == true);
163            assert(iObj == Dest.GetMatchedIndex(0, iObj));
164            // and: 1 -> 0
165            assert(Dest.IsMatched(1, iObj) == true);
166            assert(iObj == Dest.GetMatchedIndex(1, iObj));
167        }
168        // make it empty
169        Dest.Empty();
170        // and check once more
171        assert(Dest.IsEmpty() == true);
172    }
173
174    {
175        // 3rd test: create a reverse matching (i, N - i - 1), copy it, and check the copy
176        SimilMatching Source(iSize, iSize);
177        // check if it is empty
178        assert(Source.IsEmpty() == true);
179        // check sizes of objects
180        assert(Source.GetObjectSize(0) == iSize);
181        assert(Source.GetObjectSize(1) == iSize);
182        // match objects (iObj, N - iObj - 1)
183        for (iObj = 0; iObj < iSize; iObj++)
184        {
185            Source.Match(0, iObj, 1, iSize - iObj - 1);
186        }
187        // check if the matching is full
188        assert(Source.IsFull() == true);
189        // now create a copy of the matching
190        SimilMatching Dest(Source);
191        // check the copy
192        // - sizes of matched object
193        assert(Dest.GetObjectSize(0) == Source.GetObjectSize(0));
194        assert(Dest.GetObjectSize(1) == Source.GetObjectSize(1));
195        // - the copy should be full, too
196        assert(Dest.IsFull() == true);
197        // - the copy should have exactly the same assignments in matching
198        for (iObj = 0; iObj < iSize; iObj++)
199        {
200            // all object should be matched!
201            // check both directions: 0 -> 1
202            assert(Dest.IsMatched(0, iObj) == true);
203            assert((iSize - iObj - 1) == Dest.GetMatchedIndex(0, iObj));
204            // and: 1 -> 0
205            assert(Dest.IsMatched(1, iObj) == true);
206            assert((iSize - iObj - 1) == Dest.GetMatchedIndex(1, iObj));
207        }
208        // make it empty
209        Dest.Empty();
210        // and check once more
211        assert(Dest.IsEmpty() == true);
212    }
213
214    {
215        // 4th test: create a sparse matching (i, 2*i), copy it and check the copy
216        SimilMatching Source(iSize, 2 * iSize);
217        // check if it is empty
218        assert(Source.IsEmpty() == true);
219        // check sizes of objects
220        assert(Source.GetObjectSize(0) == iSize);
221        assert(Source.GetObjectSize(1) == 2 * iSize);
222        // match objects (iObj, 2 * iObj)
223        for (iObj = 0; iObj < iSize; iObj++)
224        {
225            Source.Match(0, iObj, 1, 2 * iObj);
226        }
227        // check if the matching is full (should be, as the smaller set is completely matched
228        assert(Source.IsFull() == true);
229        // now create a copy of the matching
230        SimilMatching Dest(Source);
231        // check the copy
232        // - sizes of matched object
233        assert(Dest.GetObjectSize(0) == Source.GetObjectSize(0));
234        assert(Dest.GetObjectSize(1) == Source.GetObjectSize(1));
235        // - the copy should be full, too
236        assert(Dest.IsFull() == true);
237        // - the copy should have exactly the same assignments in matching
238        for (iObj = 0; iObj < iSize; iObj++)
239        {
240            // check both directions: 0 -> 1
241            // (all matched, (iObj, 2 * iObj))
242            assert(Dest.IsMatched(0, iObj) == true);
243            assert((2 * iObj) == Dest.GetMatchedIndex(0, iObj));
244            // and direction; 1 -> 0
245            // (only even are matched, ( 2 * iObj, iObj))
246            // for 2 * iObj (which are even): matched
247            assert(Dest.IsMatched(1, 2 * iObj) == true);
248            assert(iObj == Dest.GetMatchedIndex(1, 2 * iObj));
249            // for 2 * iObj + 1 (which are odd): unmatched
250            assert(Dest.IsMatched(1, 2 * iObj + 1) == false);
251        }
252        // make it empty
253        Dest.Empty();
254        // and check once more
255        assert(Dest.IsEmpty() == true);
256    }
257
258    return 0;
259}
260
261/** Tests different matchings.
262        @return 0 if OK. Errors break execution (assert())
263 */
264int TestMatching()
265{
266    // define size used by method
267    const int nSize1 = 10;
268    // some loop counters
269    int i;
270
271    // first check some symmetric matching
272    SimilMatching match1(nSize1, nSize1);
273
274    // matching is empty
275    assert(match1.IsEmpty() == true);
276
277    // create matching - (i,i)
278    for (i = 0; i < nSize1; i++)
279    {
280
281        // matching is not full
282        assert(match1.IsFull() == false);
283
284        // these are not matched yet
285        assert(match1.IsMatched(0, i) == false);
286        assert(match1.IsMatched(1, i) == false);
287
288        // now - match!
289        match1.Match(0, i, 1, i);
290
291        // matching is not empty
292        assert(match1.IsEmpty() == false);
293
294        // now they are matched
295        assert(match1.IsMatched(0, i) == true);
296        assert(match1.IsMatched(1, i) == true);
297
298        // check the matched index for object 0 and 1
299        assert(match1.GetMatchedIndex(0, i) == i);
300        assert(match1.GetMatchedIndex(1, i) == i);
301    }
302
303    // now matching have to be full
304    assert(match1.IsFull() == true);
305
306    // check some symmetric matching
307    SimilMatching match2(nSize1, nSize1);
308
309    // matching is empty
310    assert(match2.IsEmpty() == true);
311
312    // create matching - (i, nSize1 - 1 - i)
313    for (i = 0; i < nSize1; i++)
314    {
315
316        // matching is not full
317        assert(match2.IsFull() == false);
318
319        // these are not matched yet
320        assert(match2.IsMatched(0, i) == false);
321        assert(match2.IsMatched(1, nSize1 - 1 - i) == false);
322
323        // now - match (but use the opposite syntax)!
324        match2.Match(1, nSize1 - 1 - i, 0, i);
325
326        // matching is not empty
327        assert(match2.IsEmpty() == false);
328
329        // now they are matched
330        assert(match2.IsMatched(0, i) == true);
331        assert(match2.IsMatched(1, nSize1 - 1 - i) == true);
332
333        // check the matched index for object 0 and 1
334        assert(match2.GetMatchedIndex(0, i) == (nSize1 - 1 - i));
335        assert(match2.GetMatchedIndex(1, nSize1 - 1 - i) == i);
336    }
337
338    // now matching have to be full
339    assert(match2.IsFull() == true);
340
341    // check some asymmnetic matching, too
342    SimilMatching match3(nSize1, 2 * nSize1);
343
344    // matching is empty
345    assert(match3.IsEmpty() == true);
346
347    // create matching - (i, 2 * i)
348    for (i = 0; i < nSize1; i++)
349    {
350
351        // matching is not full
352        assert(match3.IsFull() == false);
353
354        // these are not matched yet
355        assert(match3.IsMatched(0, i) == false);
356        assert(match3.IsMatched(1, 2 * i) == false);
357
358        // now - match (but use the opposite syntax)!
359        match3.Match(1, 2 * i, 0, i);
360
361        // matching is not empty
362        assert(match3.IsEmpty() == false);
363
364        // now they are matched
365        assert(match3.IsMatched(0, i) == true);
366        assert(match3.IsMatched(1, 2 * i) == true);
367
368        // but the odd elements of object 1 are not matched
369        assert(match3.IsMatched(1, 2 * i + 1) == false);
370
371        // check the matched index for object 0 and 1
372        assert(match3.GetMatchedIndex(0, i) == 2 * i);
373        assert(match3.GetMatchedIndex(1, 2 * i) == i);
374    }
375
376    // now matching have to be full (because the smallest object has all elements matched).
377    assert(match3.IsFull() == true);
378
379    return 0;
380}
381
382/** Defines all tests of SimilMatching class.
383        @return 0 if tests passed, (-1) if there were errors.
384 */
385int main(int argc, char *argv[])
386{
387    // assume that the result of tests is OK
388    bool bResultOK = true;
389
390    // run construction test
391    if (TestConstruction() != 0)
392    {
393        bResultOK = false;
394    }
395
396    // run sizes test
397    if (TestSizes() != 0)
398    {
399        bResultOK = false;
400    }
401
402    // run matching test
403    if (TestMatching() != 0)
404    {
405        bResultOK = false;
406    }
407
408    // run a copy constructor test
409    if (TestCopyConstructor() != 0)
410    {
411        bResultOK = false;
412    }
413
414    // print proper message about tests status and return
415    if (bResultOK)
416    {
417        PrintPassedMessage();
418        return 0;
419    }
420    else
421    {
422        PrintErrorMessage();
423        return (-1);
424    }
425}
Note: See TracBrowser for help on using the repository browser.