TestMap.cpp 8.24 KB
Newer Older
edrezen's avatar
edrezen committed
1
/*****************************************************************************
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 *   GATB : Genome Assembly Tool Box
 *   Copyright (C) 2014  R.Chikhi, G.Rizk, E.Drezen
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/
edrezen's avatar
edrezen committed
18
19
20
21
22

#include <CppunitCommon.hpp>

#include <gatb/tools/designpattern/api/Iterator.hpp>
#include <gatb/tools/collections/impl/OAHash.hpp>
edrezen's avatar
edrezen committed
23
#include <gatb/tools/collections/impl/MapMPHF.hpp>
edrezen's avatar
edrezen committed
24
25
26
27
#include <gatb/tools/math/NativeInt64.hpp>
#include <gatb/tools/math/NativeInt128.hpp>
#include <gatb/tools/math/LargeInt.hpp>
#include <gatb/tools/misc/api/Macros.hpp>
edrezen's avatar
edrezen committed
28
#include <gatb/tools/misc/api/Abundance.hpp>
edrezen's avatar
edrezen committed
29
#include <gatb/system/api/Exception.hpp>
edrezen's avatar
edrezen committed
30
#include <gatb/tools/storage/impl/Storage.hpp>
edrezen's avatar
edrezen committed
31
32
33
34
35
36
37
38
39
40
41
42

#include <list>
#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */
#include <memory>

using namespace std;

using namespace gatb::core::tools::dp;
using namespace gatb::core::tools::collections;
using namespace gatb::core::tools::collections::impl;
using namespace gatb::core::tools::math;
edrezen's avatar
edrezen committed
43
using namespace gatb::core::tools::misc;
edrezen's avatar
edrezen committed
44
45
using namespace gatb::core::tools::storage;
using namespace gatb::core::tools::storage::impl;
edrezen's avatar
edrezen committed
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using namespace gatb::core::system;

/********************************************************************************/
namespace gatb  {  namespace tests  {
/********************************************************************************/

/********************************************************************************/

/** \brief Test class for operating system operations
 */
class TestMap : public Test
{
    /********************************************************************************/
    CPPUNIT_TEST_SUITE_GATB (TestMap);

        CPPUNIT_TEST_GATB (checkOAHash);
edrezen's avatar
edrezen committed
62
        CPPUNIT_TEST_GATB (checkMapMPHF);
edrezen's avatar
edrezen committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78

    CPPUNIT_TEST_SUITE_GATB_END();

public:

    /********************************************************************************/
    void setUp    ()  {  srand (time(NULL));  }
    void tearDown ()  {}

    /********************************************************************************/
    template<typename T>
    void checkOAHash_aux (size_t maxMemory)
    {
        /** We create a hash with a maximum memory size. */
        OAHash <T> hash (maxMemory);

79
80
        T badKey;
        badKey.setVal(hash.getMaxNbItems() + 100);
edrezen's avatar
edrezen committed
81
82

        /** We insert the maximum number of items. */
83
84
85
86
        for (int i=1; i<=hash.getMaxNbItems(); i++)  {  
            T idx; idx.setVal(i);
            CPPUNIT_ASSERT_NO_THROW (hash.increment (idx));  
        }
edrezen's avatar
edrezen committed
87
88
89
90
91

        /** We add a new key => we should get an exception. */
        CPPUNIT_ASSERT_THROW (hash.increment (badKey), core::system::Exception);

        /** We check that we have all the required keys. */
92
93
94
95
        for (int i=1; i<=hash.getMaxNbItems(); i++)  {  
            T idx; idx.setVal(i);
            CPPUNIT_ASSERT (hash.get (idx) == true);  
        }
edrezen's avatar
edrezen committed
96
97
98
99
100

        /** We check that we don't have an non registered key. */
        CPPUNIT_ASSERT (hash.get (badKey) == false);

        /** We iterate the map. */
101
        Iterator <Abundance<T> >* it = hash.iterator();
edrezen's avatar
edrezen committed
102
103
104
105
106
107
        LOCAL (it);

        size_t nbItems = 0;
        for (it->first(); !it->isDone(); it->next(), nbItems++)
        {
            /** All abundances should be one. */
108
            CPPUNIT_ASSERT (it->item().abundance == 1);
edrezen's avatar
edrezen committed
109
110
        }

111
        CPPUNIT_ASSERT ((int)nbItems == hash.getMaxNbItems());
edrezen's avatar
edrezen committed
112
113
114
115
116
117
118
119
120
121
    }

    /********************************************************************************/
    void checkOAHash ()
    {
        size_t table[] = { 1024, 10*1024, 100*1024, 1000*1024};

        for (size_t i=0; i<ARRAY_SIZE(table); i++)
        {
            checkOAHash_aux<NativeInt64>  (table[i]);
edrezen's avatar
edrezen committed
122
    #if INT128_FOUND == 1
edrezen's avatar
edrezen committed
123
124
125
126
127
128
129
            checkOAHash_aux<NativeInt128> (table[i]);
    #endif
            checkOAHash_aux<LargeInt<3> >  (table[i]);
            checkOAHash_aux<LargeInt<4> >  (table[i]);
            checkOAHash_aux<LargeInt<5> >  (table[i]);
        }
    }
edrezen's avatar
edrezen committed
130
131
132
133
134
135
136
137
138
139
140
141
142

    /********************************************************************************/
    static void checkMapMPHF_progress (size_t round, size_t initial, size_t remaining)
    {
    }

    /** */
    void checkMapMPHF ()
    {
        if (MapMPHF<NativeInt8,float>::enabled == false) { return; }

        float val = 0;

edrezen's avatar
edrezen committed
143
144
145
146
147
148
149
150
        u_int8_t keysValue[] = {14, 35, 1, 9, 65, 37, 12, 24, 98, 124, 32};

        /** We create a file with some keys (coded as NativeInt8 values). */
        const char* filename = "keys";
        BagFile<NativeInt8> keysFile (filename);
        for (size_t i=0; i<ARRAY_SIZE(keysValue); i++)  {  keysFile.insert (keysValue[i]); }
        keysFile.flush();

edrezen's avatar
edrezen committed
151
        /** We create some Iterable for our keys. */
edrezen's avatar
edrezen committed
152
153
154
        IterableFile<NativeInt8> keys(filename);

        CPPUNIT_ASSERT (keys.getNbItems() == ARRAY_SIZE(keysValue));
edrezen's avatar
edrezen committed
155
156
157
158
159
160
161

        /** We create a map for our keys; the values are floats. */
        MapMPHF <NativeInt8, float> map1;

        /** We build the hash function for the given keys. */
        map1.build (keys);

162
        CPPUNIT_ASSERT (map1.size() == (size_t)keys.getNbItems());
edrezen's avatar
edrezen committed
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192

        /** We populate the values. */
        Iterator<NativeInt8>* itKeys = keys.iterator();   LOCAL (itKeys);
        for (itKeys->first(); !itKeys->isDone(); itKeys->next(), val++)
        {
            /** We change the value for the current key. */
            map1[itKeys->item()] = val;
        }

        /** We check the values. */
        val = 0;
        for (itKeys->first(); !itKeys->isDone(); itKeys->next(), val++)
        {
            /** We check the value for the current key. */
            CPPUNIT_ASSERT (map1[itKeys->item()] == val);
        }

        /** We create a storage object. */
        Storage* storage = StorageFactory(STORAGE_HDF5).create ("map", true, false);
        LOCAL (storage);

        /** We save the map. */
        size_t writeLen = map1.save (storage->root(), "mphf");

        /** We create another map. */
        MapMPHF <NativeInt8, float> map2;

        /** We load the hash function. */
        map2.load (storage->root(), "mphf");

193
        CPPUNIT_ASSERT (map2.size() == (size_t)keys.getNbItems());
edrezen's avatar
edrezen committed
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225

        /** We populate the values. */
        val = 0;
        for (itKeys->first(); !itKeys->isDone(); itKeys->next(), val++)
        {
            /** We change the value for the current key. */
            map2[itKeys->item()] = val;
        }

        /** We check the values. */
        val = 0;
        for (itKeys->first(); !itKeys->isDone(); itKeys->next(), val++)
        {
            /** We check the value for the current key. */
            CPPUNIT_ASSERT (map2[itKeys->item()] == val);
        }

        /** We compare the values of the two maps. */
        for (itKeys->first(); !itKeys->isDone(); itKeys->next(), val++)
        {
            /** We check the value for the current key. */
            CPPUNIT_ASSERT (map1[itKeys->item()] == map2[itKeys->item()]);
        }

        /** We compare the values of the two maps (index iteration) */
        CPPUNIT_ASSERT (map1.size() == map2.size());
        for (size_t i=0; i<map1.size(); i++)
        {
            /** We check the value for the current key. */
            CPPUNIT_ASSERT (map1.at(i) == map2.at(i));
        }
    }
edrezen's avatar
edrezen committed
226
227
228
229
};

/********************************************************************************/

230
231
232
//Temporary desactivate TestMap (for continuous integration purpose)
//CPPUNIT_TEST_SUITE_REGISTRATION      (TestMap);
//CPPUNIT_TEST_SUITE_REGISTRATION_GATB (TestMap);
edrezen's avatar
edrezen committed
233
234
235
236

/********************************************************************************/
} } /* end of namespaces. */
/********************************************************************************/