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

Last change on this file since 1288 was 1044, checked in by oriona, 4 years ago

Similarity measures code refactored. Distribution-based similarity measure added.

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
[1044]5#include "frams/model/similarity/simil-match.h"
[349]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        {
[1044]44            assert(match1.isMatched(j, i) == false);
[349]45        }
46    }
47
48    // test if all are unmatched - new method
[1044]49    assert(match1.isEmpty() == true);
[349]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    {
[1044]57        assert(match2.isMatched(0, i) == false);
[349]58    }
59    // check if all elements are unmatched yet in object 1
60    for (i = 0; i < nSize2; i++)
61    {
[1044]62        assert(match2.isMatched(1, i) == false);
[349]63    }
64
65    // test if all are unmatched - new method
[1044]66    assert(match2.isEmpty() == true);
[349]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
[1044]91            assert(pMatching->getObjectSize(0) == i);
92            assert(pMatching->getObjectSize(1) == j);
[349]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
[1044]116        assert(Source.isEmpty() == true);
[349]117        // check sizes of objects
[1044]118        assert(Source.getObjectSize(0) == iSize);
119        assert(Source.getObjectSize(1) == iSize);
[349]120        // create a copy of this matching
121        SimilMatching Dest(Source);
122        // check the copy:
123        // - sizes of matched object
[1044]124        assert(Dest.getObjectSize(0) == Source.getObjectSize(0));
125        assert(Dest.getObjectSize(1) == Source.getObjectSize(1));
[349]126        // - the copy should be empty, too
[1044]127        assert(Dest.isEmpty() == true);
[349]128        // make it empty once again
[1044]129        Dest.empty();
[349]130        // and check once more
[1044]131        assert(Dest.isEmpty() == true);
[349]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
[1044]138        assert(Source.isEmpty() == true);
[349]139        // check sizes of objects
[1044]140        assert(Source.getObjectSize(0) == iSize);
141        assert(Source.getObjectSize(1) == iSize);
[349]142        // match objects (iObj, iObj)
143        for (iObj = 0; iObj < iSize; iObj++)
144        {
[1044]145            Source.match(0, iObj, 1, iObj);
[349]146        }
147        // check if the matching is full
[1044]148        assert(Source.isFull() == true);
[349]149        // now create a copy of the matching
150        SimilMatching Dest(Source);
151        // check the copy
152        // - sizes of matched object
[1044]153        assert(Dest.getObjectSize(0) == Source.getObjectSize(0));
154        assert(Dest.getObjectSize(1) == Source.getObjectSize(1));
[349]155        // - the copy should be full, too
[1044]156        assert(Dest.isFull() == true);
[349]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
[1044]162            assert(Dest.isMatched(0, iObj) == true);
163            assert(iObj == Dest.getMatchedIndex(0, iObj));
[349]164            // and: 1 -> 0
[1044]165            assert(Dest.isMatched(1, iObj) == true);
166            assert(iObj == Dest.getMatchedIndex(1, iObj));
[349]167        }
168        // make it empty
[1044]169        Dest.empty();
[349]170        // and check once more
[1044]171        assert(Dest.isEmpty() == true);
[349]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
[1044]178        assert(Source.isEmpty() == true);
[349]179        // check sizes of objects
[1044]180        assert(Source.getObjectSize(0) == iSize);
181        assert(Source.getObjectSize(1) == iSize);
[349]182        // match objects (iObj, N - iObj - 1)
183        for (iObj = 0; iObj < iSize; iObj++)
184        {
[1044]185            Source.match(0, iObj, 1, iSize - iObj - 1);
[349]186        }
187        // check if the matching is full
[1044]188        assert(Source.isFull() == true);
[349]189        // now create a copy of the matching
190        SimilMatching Dest(Source);
191        // check the copy
192        // - sizes of matched object
[1044]193        assert(Dest.getObjectSize(0) == Source.getObjectSize(0));
194        assert(Dest.getObjectSize(1) == Source.getObjectSize(1));
[349]195        // - the copy should be full, too
[1044]196        assert(Dest.isFull() == true);
[349]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
[1044]202            assert(Dest.isMatched(0, iObj) == true);
203            assert((iSize - iObj - 1) == Dest.getMatchedIndex(0, iObj));
[349]204            // and: 1 -> 0
[1044]205            assert(Dest.isMatched(1, iObj) == true);
206            assert((iSize - iObj - 1) == Dest.getMatchedIndex(1, iObj));
[349]207        }
208        // make it empty
[1044]209        Dest.empty();
[349]210        // and check once more
[1044]211        assert(Dest.isEmpty() == true);
[349]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
[1044]218        assert(Source.isEmpty() == true);
[349]219        // check sizes of objects
[1044]220        assert(Source.getObjectSize(0) == iSize);
221        assert(Source.getObjectSize(1) == 2 * iSize);
[349]222        // match objects (iObj, 2 * iObj)
223        for (iObj = 0; iObj < iSize; iObj++)
224        {
[1044]225            Source.match(0, iObj, 1, 2 * iObj);
[349]226        }
227        // check if the matching is full (should be, as the smaller set is completely matched
[1044]228        assert(Source.isFull() == true);
[349]229        // now create a copy of the matching
230        SimilMatching Dest(Source);
231        // check the copy
232        // - sizes of matched object
[1044]233        assert(Dest.getObjectSize(0) == Source.getObjectSize(0));
234        assert(Dest.getObjectSize(1) == Source.getObjectSize(1));
[349]235        // - the copy should be full, too
[1044]236        assert(Dest.isFull() == true);
[349]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))
[1044]242            assert(Dest.isMatched(0, iObj) == true);
243            assert((2 * iObj) == Dest.getMatchedIndex(0, iObj));
[349]244            // and direction; 1 -> 0
245            // (only even are matched, ( 2 * iObj, iObj))
246            // for 2 * iObj (which are even): matched
[1044]247            assert(Dest.isMatched(1, 2 * iObj) == true);
248            assert(iObj == Dest.getMatchedIndex(1, 2 * iObj));
[349]249            // for 2 * iObj + 1 (which are odd): unmatched
[1044]250            assert(Dest.isMatched(1, 2 * iObj + 1) == false);
[349]251        }
252        // make it empty
[1044]253        Dest.empty();
[349]254        // and check once more
[1044]255        assert(Dest.isEmpty() == true);
[349]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
[1044]275    assert(match1.isEmpty() == true);
[349]276
277    // create matching - (i,i)
278    for (i = 0; i < nSize1; i++)
279    {
280
281        // matching is not full
[1044]282        assert(match1.isFull() == false);
[349]283
284        // these are not matched yet
[1044]285        assert(match1.isMatched(0, i) == false);
286        assert(match1.isMatched(1, i) == false);
[349]287
288        // now - match!
[1044]289        match1.match(0, i, 1, i);
[349]290
291        // matching is not empty
[1044]292        assert(match1.isEmpty() == false);
[349]293
294        // now they are matched
[1044]295        assert(match1.isMatched(0, i) == true);
296        assert(match1.isMatched(1, i) == true);
[349]297
298        // check the matched index for object 0 and 1
[1044]299        assert(match1.getMatchedIndex(0, i) == i);
300        assert(match1.getMatchedIndex(1, i) == i);
[349]301    }
302
303    // now matching have to be full
[1044]304    assert(match1.isFull() == true);
[349]305
306    // check some symmetric matching
307    SimilMatching match2(nSize1, nSize1);
308
309    // matching is empty
[1044]310    assert(match2.isEmpty() == true);
[349]311
312    // create matching - (i, nSize1 - 1 - i)
313    for (i = 0; i < nSize1; i++)
314    {
315
316        // matching is not full
[1044]317        assert(match2.isFull() == false);
[349]318
319        // these are not matched yet
[1044]320        assert(match2.isMatched(0, i) == false);
321        assert(match2.isMatched(1, nSize1 - 1 - i) == false);
[349]322
323        // now - match (but use the opposite syntax)!
[1044]324        match2.match(1, nSize1 - 1 - i, 0, i);
[349]325
326        // matching is not empty
[1044]327        assert(match2.isEmpty() == false);
[349]328
329        // now they are matched
[1044]330        assert(match2.isMatched(0, i) == true);
331        assert(match2.isMatched(1, nSize1 - 1 - i) == true);
[349]332
333        // check the matched index for object 0 and 1
[1044]334        assert(match2.getMatchedIndex(0, i) == (nSize1 - 1 - i));
335        assert(match2.getMatchedIndex(1, nSize1 - 1 - i) == i);
[349]336    }
337
338    // now matching have to be full
[1044]339    assert(match2.isFull() == true);
[349]340
341    // check some asymmnetic matching, too
342    SimilMatching match3(nSize1, 2 * nSize1);
343
344    // matching is empty
[1044]345    assert(match3.isEmpty() == true);
[349]346
347    // create matching - (i, 2 * i)
348    for (i = 0; i < nSize1; i++)
349    {
350
351        // matching is not full
[1044]352        assert(match3.isFull() == false);
[349]353
354        // these are not matched yet
[1044]355        assert(match3.isMatched(0, i) == false);
356        assert(match3.isMatched(1, 2 * i) == false);
[349]357
358        // now - match (but use the opposite syntax)!
[1044]359        match3.match(1, 2 * i, 0, i);
[349]360
361        // matching is not empty
[1044]362        assert(match3.isEmpty() == false);
[349]363
364        // now they are matched
[1044]365        assert(match3.isMatched(0, i) == true);
366        assert(match3.isMatched(1, 2 * i) == true);
[349]367
368        // but the odd elements of object 1 are not matched
[1044]369        assert(match3.isMatched(1, 2 * i + 1) == false);
[349]370
371        // check the matched index for object 0 and 1
[1044]372        assert(match3.getMatchedIndex(0, i) == 2 * i);
373        assert(match3.getMatchedIndex(1, 2 * i) == i);
[349]374    }
375
376    // now matching have to be full (because the smallest object has all elements matched).
[1044]377    assert(match3.isFull() == true);
[349]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    }
[1044]425}
Note: See TracBrowser for help on using the repository browser.