RawDatabaseTableTestSuite.h 35.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
/***************************************************************************
 *   Copyright (C) 2007 by Lionel Torti                                    *
 *   {prenom.nom}@lip6.fr                                                  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it wil be useful,        *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <cxxtest/AgrumTestSuite.h>
#include <cxxtest/testsuite_utils.h>
#include <ressources/myalloc.h>
#include <iostream>

#include <agrum/core/thread.h>
#include <agrum/learning/database/rawDatabaseTable.h>

namespace gum_tests {

  class RawDatabaseTableTestSuite : public CxxTest::TestSuite {
    public:
    void test_db1() {
      gum::learning::RawDatabaseTable<> database;
35 36 37 38 39 40 41 42 43 44 45
      TS_ASSERT(database.content().size() == 0);
      TS_ASSERT(database.variableNames().size() == 0);

      database.setVariableNames({"v1", "v2", "v3"});
      TS_ASSERT(database.variableNames().size() == 3);
      TS_ASSERT(database.nbVariables() == 3);
      TS_ASSERT(database.variableNames()[0] == "v1");
      TS_GUM_ASSERT_THROWS_NOTHING(database.setVariableNames({"x1", "x2"}, false));
      TS_GUM_ASSERT_THROWS_NOTHING(database.setVariableNames({"x1", "x2"}, true));
      database.setVariableNames({"x1", "x2", "x3"}, false);
      TS_ASSERT(database.variableNames()[0] == "x1");
46

47
      const auto xmiss = gum::learning::RawDatabaseTable<>::IsMissing::False;
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
      gum::learning::DBRow< gum::learning::DBCell > row(3,
                                                        gum::learning::DBCell(2));
      database.insertRow(row, xmiss);
      TS_ASSERT(database.content().size() == 1);
      TS_ASSERT_THROWS(database.setVariableNames({"x1", "x2"}, false),
                       gum::SizeError);

      gum::learning::RawDatabaseTable<> db(database);
      TS_ASSERT(db.variableNames().size() == 3);
      TS_ASSERT(db.nbVariables() == 3);
      TS_ASSERT(db.content().size() == 1);

      gum::learning::RawDatabaseTable<> db2(
        db, std::allocator< gum::learning::DBTranslatedValue >());
      TS_ASSERT(db2.variableNames().size() == 3);
      TS_ASSERT(db2.nbVariables() == 3);
      TS_ASSERT(db2.content().size() == 1);

      gum::learning::RawDatabaseTable<> db3(std::move(db2));
      TS_ASSERT(db3.variableNames().size() == 3);
      TS_ASSERT(db3.nbVariables() == 3);
      TS_ASSERT(db3.content().size() == 1);

      database.insertRow(row, xmiss);
      gum::learning::RawDatabaseTable<> db4(
        std::move(database), std::allocator< gum::learning::DBTranslatedValue >());
      TS_ASSERT(db4.variableNames().size() == 3);
      TS_ASSERT(db4.nbVariables() == 3);
      TS_ASSERT(db4.content().size() == 2);

      TS_ASSERT(db.content().size() == 1);
79
      db = db4;
80
      TS_ASSERT(db.content().size() == 2);
81 82

      db2 = db4;
83 84 85
      TS_ASSERT(db2.variableNames().size() == 3);
      TS_ASSERT(db2.nbVariables() == 3);
      TS_ASSERT(db2.content().size() == 2);
86 87 88 89
    }


    void test_db1_bis() {
90 91 92 93 94
      const std::vector< std::string >           empty_miss;
      std::vector< std::string >                 vnames{"v1", "v2", "v3"};
      gum::learning::RawDatabaseTable< MyAlloc > database(empty_miss, vnames);
      TS_ASSERT(database.variableNames().size() == 3);
      TS_ASSERT(database.nbVariables() == 3);
95

96
      const auto xmiss =
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
        gum::learning::RawDatabaseTable< MyAlloc >::IsMissing::False;
      gum::learning::DBRow< gum::learning::DBCell, MyAlloc > row(
        3, gum::learning::DBCell(2));
      database.insertRow(row, xmiss);
      TS_ASSERT(database.content().size() == 1);

      gum::learning::RawDatabaseTable< MyAlloc > db(database);
      TS_ASSERT(db.variableNames().size() == 3);
      TS_ASSERT(db.nbVariables() == 3);
      TS_ASSERT(db.content().size() == 1);

      gum::learning::RawDatabaseTable< MyAlloc > db2(
        db, MyAlloc< gum::learning::DBTranslatedValue >());
      TS_ASSERT(db2.variableNames().size() == 3);
      TS_ASSERT(db2.nbVariables() == 3);
      TS_ASSERT(db2.content().size() == 1);

      gum::learning::RawDatabaseTable< MyAlloc > db3(std::move(db2));
      TS_ASSERT(db3.variableNames().size() == 3);
      TS_ASSERT(db3.nbVariables() == 3);
      TS_ASSERT(db3.content().size() == 1);

      database.insertRow(row, xmiss);
      gum::learning::RawDatabaseTable< MyAlloc > db4(
        std::move(database), MyAlloc< gum::learning::DBTranslatedValue >());
      TS_ASSERT(db4.variableNames().size() == 3);
      TS_ASSERT(db4.nbVariables() == 3);
      TS_ASSERT(db4.content().size() == 2);

      TS_ASSERT(db.content().size() == 1);
127
      db = db4;
128
      TS_ASSERT(db.content().size() == 2);
129 130

      db2 = db4;
131 132 133
      TS_ASSERT(db2.variableNames().size() == 3);
      TS_ASSERT(db2.nbVariables() == 3);
      TS_ASSERT(db2.content().size() == 2);
134
    }
135

136 137 138

    void test_db2() {
      gum::learning::RawDatabaseTable<> database;
139 140 141 142 143 144 145 146 147 148 149
      TS_ASSERT(database.content().size() == 0);
      TS_ASSERT(database.variableNames().size() == 0);

      std::vector< std::string, MyAlloc< std::string > > vect1{"v1", "v2"};
      database.setVariableNames(vect1);
      TS_ASSERT(database.variableNames().size() == 2);
      TS_ASSERT(database.nbVariables() == 2);
      std::vector< std::string > vect{"v1", "v2", "v3"};
      database.setVariableNames(vect);
      TS_ASSERT(database.variableNames().size() == 3);
      TS_ASSERT(database.nbVariables() == 3);
150 151

      const auto xmiss = gum::learning::RawDatabaseTable<>::IsMissing::False;
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
      gum::learning::DBRow< gum::learning::DBCell > row(3,
                                                        gum::learning::DBCell(2));
      database.insertRow(row, xmiss);
      TS_ASSERT(database.content().size() == 1);
      std::vector< std::string > badvect{"v1", "v2", "v3", "v4"};
      TS_ASSERT_THROWS(database.setVariableNames(badvect), gum::SizeError);

      typename gum::learning::RawDatabaseTable<>::HandlerSafe handler(database);
      typename gum::learning::RawDatabaseTable<>::Handler     uhandler(database);
      database.insertRow(row, xmiss);
      database.insertRow(row, xmiss);
      database.insertRow(row, xmiss);
      database.insertRow(row, xmiss);
      database.insertRow(std::move(row), xmiss);
      TS_ASSERT(database.content().size() == 6);

      TS_ASSERT(handler.range().second == 6);
      TS_ASSERT(handler.size() == 6);
      TS_ASSERT(handler.DBSize() == 6);
      TS_ASSERT(uhandler.size() == 1);
      TS_ASSERT(uhandler.DBSize() == 6);
      handler.setRange(1, 4);
      TS_ASSERT(handler.size() == 3);
      TS_ASSERT(handler.DBSize() == 6);
      TS_ASSERT(handler.numRow() == 0);
      uhandler.setRange(1, 4);
      TS_ASSERT(uhandler.size() == 3);
      TS_ASSERT(uhandler.DBSize() == 6);
      TS_ASSERT(uhandler.numRow() == 0);
181
      handler.nextRow();
182
      TS_ASSERT(handler.numRow() == 1);
183

184 185 186 187 188
      auto handler2(handler);
      TS_ASSERT(handler2.size() == 3);
      TS_ASSERT(handler2.DBSize() == 6);
      TS_ASSERT(handler2.range().second == 4);
      TS_ASSERT(handler2.numRow() == 1);
189
      handler2.nextRow();
190
      TS_ASSERT(handler2.numRow() == 2);
191

192 193 194 195 196
      auto uhandler2(uhandler);
      TS_ASSERT(uhandler2.size() == 3);
      TS_ASSERT(uhandler2.DBSize() == 6);
      TS_ASSERT(uhandler2.range().second == 4);
      TS_ASSERT(uhandler2.numRow() == 0);
197
      uhandler2.nextRow();
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
      TS_ASSERT(uhandler2.numRow() == 1);

      auto handler3(std::move(handler2));
      TS_ASSERT(handler3.size() == 3);
      TS_ASSERT(handler3.DBSize() == 6);
      TS_ASSERT(handler3.range().first == 1);
      TS_ASSERT(handler3.range().second == 4);
      TS_ASSERT(handler3.numRow() == 2);

      auto uhandler3(std::move(uhandler2));
      TS_ASSERT(uhandler3.size() == 3);
      TS_ASSERT(uhandler3.DBSize() == 6);
      TS_ASSERT(uhandler3.range().first == 1);
      TS_ASSERT(uhandler3.range().second == 4);
      TS_ASSERT(uhandler3.numRow() == 1);
213 214

      handler2 = handler;
215 216 217 218
      TS_ASSERT(handler.range().second == 4);
      TS_ASSERT(handler.size() == 3);
      TS_ASSERT(handler.DBSize() == 6);
      TS_ASSERT(handler.numRow() == 1);
219 220

      uhandler2 = uhandler;
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
      TS_ASSERT(uhandler.range().second == 4);
      TS_ASSERT(uhandler.size() == 3);
      TS_ASSERT(uhandler.DBSize() == 6);
      TS_ASSERT(uhandler.numRow() == 0);

      handler2 = std::move(handler3);
      TS_ASSERT(handler2.size() == 3);
      TS_ASSERT(handler2.DBSize() == 6);
      TS_ASSERT(handler2.range().second == 4);
      TS_ASSERT(handler2.numRow() == 2);

      uhandler2 = std::move(uhandler3);
      TS_ASSERT(uhandler2.size() == 3);
      TS_ASSERT(uhandler2.DBSize() == 6);
      TS_ASSERT(uhandler2.range().second == 4);
      TS_ASSERT(uhandler2.numRow() == 1);

      auto&                                       row2 = handler2.rowSafe();
      const std::vector< gum::learning::DBCell >& xrow = row2.row();
      TS_ASSERT(xrow.size() == 3);
      TS_ASSERT(xrow[1] == 2);

      TS_ASSERT(handler2.hasRows() == true);
244
      handler2.nextRow();
245 246 247 248
      TS_ASSERT(handler2.hasRows() == false);
      handler2.reset();
      TS_ASSERT(handler2.hasRows() == true);
      TS_ASSERT(handler2.numRow() == 0);
249 250

      const auto& vars = handler2.variableNames();
251 252
      TS_ASSERT(vars[1] == "v2");
      TS_ASSERT(handler2.nbVariables() == 3);
253 254

      uhandler2.nextRow();
255 256 257 258
      auto&       urow2 = uhandler2.rowSafe();
      const auto& uxrow = urow2.row();
      TS_ASSERT(uxrow.size() == 3);
      TS_ASSERT(uxrow[1] == 2);
259

260
      TS_ASSERT(uhandler2.hasRows() == true);
261
      uhandler2.nextRow();
262 263 264 265
      TS_ASSERT(uhandler2.hasRows() == false);
      uhandler2.reset();
      TS_ASSERT(uhandler2.hasRows() == true);
      TS_ASSERT(uhandler2.numRow() == 0);
266 267

      const auto& uvars = uhandler2.variableNames();
268 269
      TS_ASSERT(uvars[1] == "v2");
      TS_ASSERT(uhandler2.nbVariables() == 3);
270 271

      // check that we cannot convert an unsafe handler into a safe one
272
      TS_ASSERT_THROWS(handler2 = uhandler2, std::bad_cast);
273
      gum::learning::RawDatabaseTable<>::Handler& uxhandler2 = handler2;
274
      TS_ASSERT_THROWS(uxhandler2 = uhandler2, std::bad_cast);
275 276
    }

277

278 279
    void test_db3() {
      gum::learning::RawDatabaseTable<> database;
280 281
      TS_ASSERT(database.content().size() == 0);
      TS_ASSERT(database.variableNames().size() == 0);
282

283 284 285 286
      std::vector< std::string, MyAlloc< std::string > > vect{"v1", "v2", "v3"};
      database.setVariableNames(vect);
      TS_ASSERT(database.variableNames().size() == 3);
      TS_ASSERT(database.nbVariables() == 3);
287 288

      const auto xmiss = gum::learning::RawDatabaseTable<>::IsMissing::False;
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
      gum::learning::DBRow< gum::learning::DBCell > row(3,
                                                        gum::learning::DBCell(2));
      database.insertRow(row, xmiss);
      TS_ASSERT(database.content().size() == 1);

      gum::learning::DBRow< gum::learning::DBCell > row2(3,
                                                         gum::learning::DBCell(5));
      database.insertRow(row2, xmiss);
      gum::learning::DBRow< gum::learning::DBCell > row3(3,
                                                         gum::learning::DBCell(3));
      database.insertRow(std::move(row3), xmiss);
      TS_ASSERT(database.content().size() == 3);

      std::vector< gum::learning::RawDatabaseTable<>::IsMissing > is_miss(
        4, gum::learning::RawDatabaseTable<>::IsMissing::False);
304
      database.insertRows(
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
        std::vector< gum::learning::DBRow< gum::learning::DBCell > >(4, row),
        is_miss);
      TS_ASSERT(database.content().size() == 7);
      std::vector< gum::learning::DBRow< gum::learning::DBCell > > vectx(4, row2);
      database.insertRows(vectx, is_miss);
      TS_ASSERT(database.content().size() == 11);
      std::vector< gum::learning::DBRow< gum::learning::DBCell > > vecty(2, row);
      database.insertRows(vecty, is_miss);
      TS_ASSERT(database.content().size() == 13);

      database.eraseRow(12);
      database.eraseRow(5);
      TS_ASSERT(database.content().size() == 11);
      database.eraseFirstRow();
      database.eraseLastRow();
      database.eraseFirstRows(2);
      TS_ASSERT(database.content().size() == 7);
      database.eraseLastRows(1);
      TS_ASSERT(database.content().size() == 6);
      database.eraseRows(2, 4);
      TS_ASSERT(database.content().size() == 4);
      database.eraseAllRows();
      TS_ASSERT(database.content().size() == 0);
      database.clear();
      TS_ASSERT(database.content().size() == 0);
330
    }
331 332


333 334
    void test_db4() {
      gum::learning::RawDatabaseTable<> database;
335 336
      TS_ASSERT(database.content().size() == 0);
      TS_ASSERT(database.variableNames().size() == 0);
337

338 339 340 341
      std::vector< std::string, MyAlloc< std::string > > vect{"v1", "v2", "v3"};
      database.setVariableNames(vect);
      TS_ASSERT(database.variableNames().size() == 3);
      TS_ASSERT(database.nbVariables() == 3);
342 343

      const auto xmiss = gum::learning::RawDatabaseTable<>::IsMissing::False;
344 345 346 347 348 349 350
      gum::learning::DBRow< gum::learning::DBCell > row(3,
                                                        gum::learning::DBCell(2));
      database.insertRow(row, xmiss);
      database.insertRow(row, xmiss);
      database.insertRow(row, xmiss);
      database.insertRow(row, xmiss);
      TS_ASSERT(database.content().size() == 4);
351 352

      gum::learning::RawDatabaseTable<> database2 = database;
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
      TS_ASSERT(database2.content().size() == 4);
      TS_ASSERT(database2.variableNames().size() == 3);
      TS_ASSERT(database2.nbVariables() == 3);

      gum::learning::DBRow< gum::learning::DBCell > row2(4,
                                                         gum::learning::DBCell(2));
      TS_ASSERT_THROWS(database2.insertRow(row2, xmiss), gum::SizeError);

      std::vector< gum::learning::RawDatabaseTable<>::IsMissing > is_miss(
        4, gum::learning::RawDatabaseTable<>::IsMissing::False);
      gum::learning::DBRow< gum::learning::DBCell > row3(3,
                                                         gum::learning::DBCell(4));
      gum::learning::DBRow< gum::learning::DBCell > row4(3,
                                                         gum::learning::DBCell(5));
      database2.insertRows({row3, row4}, is_miss);
      typename gum::learning::RawDatabaseTable<>::HandlerSafe handler2(database2);
      typename gum::learning::RawDatabaseTable<>::HandlerSafe handler3(database2);
      handler2.setRange(4, 6);
      handler3.setRange(5, 6);
      TS_ASSERT(handler2.row()[0] == 4);
      TS_ASSERT(handler3.row()[0] == 5);
374
      database2.eraseLastRow();
375 376 377 378 379 380 381 382 383 384
      TS_ASSERT(handler2.row()[0] == 4);
      TS_ASSERT_THROWS(handler3.rowSafe()[0], gum::OutOfBounds);
      database2.insertRow(row4, xmiss);

      typename gum::learning::RawDatabaseTable<>::Handler uhandler2(database2);
      typename gum::learning::RawDatabaseTable<>::Handler uhandler3(database2);
      uhandler2.setRange(4, 6);
      uhandler3.setRange(5, 6);
      TS_ASSERT(uhandler2.row()[0] == 4);
      TS_ASSERT(uhandler3.row()[0] == 5);
385
      database2.eraseLastRow();
386 387 388 389 390 391 392 393 394 395 396 397
      TS_ASSERT(uhandler2.row()[0] == 4);
      TS_ASSERT(uhandler3.size() == 1);

      gum::learning::RawDatabaseTable<> database3(std::move(database2));
      database2 = std::move(database);
      TS_ASSERT(database2.content().size() == 4);
      TS_ASSERT(database.content().size() == 0);
      database = std::move(database3);
      TS_ASSERT(database.content().size() == 5);

      handler2 = database.handlerSafe();
      TS_ASSERT(handler2.range().second == 5);
398 399 400
      handler2.nextRow();
      handler2.nextRow();
      handler2.nextRow();
401 402 403
      handler3 = std::move(handler2);
      TS_ASSERT(handler3.range().second == 5);
      TS_ASSERT(handler3.hasRows());
404
      handler3.nextRow();
405
      TS_ASSERT(handler3.hasRows());
406
      handler3.nextRow();
407
      TS_ASSERT(handler3.hasRows() == false);
408 409
    }

410

411 412
    void test_db5() {
      gum::learning::RawDatabaseTable<> database;
413 414
      TS_ASSERT(database.content().size() == 0);
      TS_ASSERT(database.variableNames().size() == 0);
415

416 417 418 419
      std::vector< std::string, MyAlloc< std::string > > vect{"v1", "v2", "v3"};
      database.setVariableNames(vect);
      TS_ASSERT(database.variableNames().size() == 3);
      TS_ASSERT(database.nbVariables() == 3);
420 421

      const auto xmiss = gum::learning::RawDatabaseTable<>::IsMissing::False;
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
      gum::learning::DBRow< gum::learning::DBCell > row(3,
                                                        gum::learning::DBCell(2));
      database.insertRow(row, xmiss);
      database.insertRow(row, xmiss);
      database.insertRow(row, xmiss);
      database.insertRow(row, xmiss);
      TS_ASSERT(database.content().size() == 4);

      std::vector< gum::learning::RawDatabaseTable<>::IsMissing > is_miss(
        4, gum::learning::RawDatabaseTable<>::IsMissing::False);
      gum::learning::DBRow< gum::learning::DBCell > row3(3,
                                                         gum::learning::DBCell(4));
      gum::learning::DBRow< gum::learning::DBCell > row4(3,
                                                         gum::learning::DBCell(5));
      database.insertRows({row3, row4}, is_miss);
      typename gum::learning::RawDatabaseTable<>::Handler handler(database);

439
      int x = 0;
440
      for (const auto& row : handler) {
441
        x++;
442
        TS_ASSERT(row.size() == 3);
443
      }
444
      TS_ASSERT(x == 6);
445

446
      handler.setRange(1, 3);
447
      x = 0;
448
      for (const auto& row : handler) {
449
        x++;
450
        TS_ASSERT(row.size() == 3);
451
      }
452
      TS_ASSERT(x == 2);
453 454 455 456 457
    }


    void test_db6() {
      gum::learning::RawDatabaseTable<> database;
458 459 460 461 462 463 464 465 466 467 468 469 470
      TS_ASSERT(database.content().size() == 0);
      TS_ASSERT(database.variableNames().size() == 0);

      database.setVariableNames({"v1", "v2", "v3"});
      TS_ASSERT(database.variableNames().size() == 3);
      TS_ASSERT(database.nbVariables() == 3);

      std::vector< std::string > row(3, "2");
      database.insertRow(row);
      TS_ASSERT(database.content().size() == 1);
      for (const auto& xrow : database) {
        for (const auto& xxx : xrow.row()) {
          TS_ASSERT(xxx == gum::learning::DBCell(2));
471 472 473
        }
      }

474 475 476 477 478 479
      std::vector< std::string, MyAlloc< std::string > > row2(3, "2");
      database.insertRow(row2);
      TS_ASSERT(database.content().size() == 2);
      for (const auto& xrow : database) {
        for (const auto& xxx : xrow.row()) {
          TS_ASSERT(xxx == gum::learning::DBCell(2));
480 481 482
        }
      }

483 484 485
      std::vector< std::string > row3(5, "3");
      for (int i = 0; i < 5; i++)
        row3[i] = std::to_string(i);
486 487 488
      database.ignoreColumn(1);
      database.ignoreColumn(3);
      database.ignoreColumn(4);
489
      TS_GUM_ASSERT_THROWS_NOTHING(database.insertRow(row3));
490 491

      database.ignoreColumn(5);
492
      TS_GUM_ASSERT_THROWS_NOTHING(database.insertRow(row3));
493 494 495

      database.ignoreColumn(6);
      database.ignoreColumn(7);
496
      TS_GUM_ASSERT_THROWS_NOTHING(database.insertRow(row3));
497 498
    }

499 500

    void test_db_parallel() {
501
      gum::learning::RawDatabaseTable<> database;
502 503
      TS_ASSERT(database.content().size() == 0);
      TS_ASSERT(database.variableNames().size() == 0);
504

505 506 507 508
      std::vector< std::string, MyAlloc< std::string > > vect{"v1", "v2", "v3"};
      database.setVariableNames(vect);
      TS_ASSERT(database.variableNames().size() == 3);
      TS_ASSERT(database.nbVariables() == 3);
509 510

      const auto xmiss = gum::learning::RawDatabaseTable<>::IsMissing::False;
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
      gum::learning::DBRow< gum::learning::DBCell > row(3,
                                                        gum::learning::DBCell(2));
      database.insertRow(row, xmiss);
      database.insertRow(row, xmiss);
      database.insertRow(row, xmiss);
      database.insertRow(row, xmiss);
      TS_ASSERT(database.content().size() == 4);

      std::vector< gum::learning::RawDatabaseTable<>::IsMissing > is_miss(
        4, gum::learning::RawDatabaseTable<>::IsMissing::False);
      gum::learning::DBRow< gum::learning::DBCell > row3(3,
                                                         gum::learning::DBCell(4));
      gum::learning::DBRow< gum::learning::DBCell > row4(3,
                                                         gum::learning::DBCell(5));
      database.insertRows({row3, row4}, is_miss);
526 527

      const unsigned int num_threads = gum::thread::getMaxNumberOfThreads();
528 529 530 531 532 533 534 535 536 537 538 539

      std::vector< gum::learning::RawDatabaseTable<>::Handler* > handlers(
        num_threads);
      std::vector< std::thread* > threads(num_threads);
      std::vector< int >          nb(num_threads);

      for (unsigned int i = 0; i < num_threads; ++i) {
        threads[i] = new std::thread(&RawDatabaseTableTestSuite::__create_handler,
                                     this,
                                     &database,
                                     &(handlers[i]),
                                     &(nb[i]));
540 541
      }

542 543
      for (unsigned int i = 0; i < num_threads; ++i) {
        threads[i]->join();
544 545
      }

546
      for (unsigned int i = 0; i < num_threads; ++i) {
547 548 549 550
        delete threads[i];
        delete handlers[i];
      }

551 552
      for (unsigned int i = 0; i < num_threads; ++i) {
        TS_ASSERT(nb[i] == 6);
553 554 555
      }
    }

556 557

    void test_db_parallel2() {
558
      gum::learning::RawDatabaseTable<> database;
559 560
      TS_ASSERT(database.content().size() == 0);
      TS_ASSERT(database.variableNames().size() == 0);
561

562 563 564 565
      std::vector< std::string, MyAlloc< std::string > > vect{"v1", "v2", "v3"};
      database.setVariableNames(vect);
      TS_ASSERT(database.variableNames().size() == 3);
      TS_ASSERT(database.nbVariables() == 3);
566 567

      const auto xmiss = gum::learning::RawDatabaseTable<>::IsMissing::False;
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
      gum::learning::DBRow< gum::learning::DBCell > row(3,
                                                        gum::learning::DBCell(2));
      database.insertRow(row, xmiss);
      database.insertRow(row, xmiss);
      database.insertRow(row, xmiss);
      database.insertRow(row, xmiss);
      TS_ASSERT(database.content().size() == 4);

      std::vector< gum::learning::RawDatabaseTable<>::IsMissing > is_miss(
        4, gum::learning::RawDatabaseTable<>::IsMissing::False);
      gum::learning::DBRow< gum::learning::DBCell > row3(3,
                                                         gum::learning::DBCell(4));
      gum::learning::DBRow< gum::learning::DBCell > row4(3,
                                                         gum::learning::DBCell(5));
      database.insertRows({row3, row4}, is_miss);
583 584

      const unsigned int num_threads = gum::thread::getMaxNumberOfThreads();
585 586 587 588 589 590 591

      std::vector< gum::learning::RawDatabaseTable<>::HandlerSafe* > handlers(
        num_threads);
      std::vector< std::thread* > threads(num_threads);
      std::vector< int >          nb(num_threads);

      for (unsigned int i = 0; i < num_threads; ++i) {
592
        threads[i] =
593 594 595 596 597
          new std::thread(&RawDatabaseTableTestSuite::__create_handler_safe,
                          this,
                          &database,
                          &(handlers[i]),
                          &(nb[i]));
598 599
      }

600 601
      for (unsigned int i = 0; i < num_threads; ++i) {
        threads[i]->join();
602 603
      }

604
      for (unsigned int i = 0; i < num_threads; ++i) {
605 606 607 608
        delete threads[i];
        delete handlers[i];
      }

609 610
      for (unsigned int i = 0; i < num_threads; ++i) {
        TS_ASSERT(nb[i] == 6);
611 612 613 614
      }
    }


615
    void test_iterators() {
616
      gum::learning::RawDatabaseTable<> database;
617 618
      TS_ASSERT(database.content().size() == 0);
      TS_ASSERT(database.variableNames().size() == 0);
619

620 621 622 623
      std::vector< std::string, MyAlloc< std::string > > vect{"v1", "v2", "v3"};
      database.setVariableNames(vect);
      TS_ASSERT(database.variableNames().size() == 3);
      TS_ASSERT(database.nbVariables() == 3);
624 625

      const auto xmiss = gum::learning::RawDatabaseTable<>::IsMissing::False;
626 627 628 629 630 631 632
      gum::learning::DBRow< gum::learning::DBCell > row(3,
                                                        gum::learning::DBCell(2));
      database.insertRow(row, xmiss);
      database.insertRow(row, xmiss);
      database.insertRow(row, xmiss);
      database.insertRow(row, xmiss);
      TS_ASSERT(database.content().size() == 4);
633 634

      int nb_col1 = 0, nb_col2 = 0;
635
      for (const auto row : database) {
636
        const auto& r = row.row();
637 638
        nb_col1 += r[0].integer();
        nb_col2 += r[1].integer();
639
      }
640 641
      TS_ASSERT(nb_col1 == 8);
      TS_ASSERT(nb_col2 == 8);
642 643 644

      nb_col1 = 0;
      nb_col2 = 0;
645
      for (auto iter = database.begin(); iter != database.end(); ++iter) {
646
        const auto& r = iter->row();
647 648
        nb_col1 += r[0].integer();
        nb_col2 += r[1].integer();
649
      }
650 651
      TS_ASSERT(nb_col1 == 8);
      TS_ASSERT(nb_col2 == 8);
652 653 654

      nb_col1 = 0;
      nb_col2 = 0;
655
      for (auto iter = database.beginSafe(); iter != database.endSafe(); ++iter) {
656
        const auto& r = (*iter).row();
657 658
        nb_col1 += r[0].integer();
        nb_col2 += r[1].integer();
659
      }
660 661 662
      TS_ASSERT(nb_col1 == 8);
      TS_ASSERT(nb_col2 == 8);

663 664
      nb_col1 = 0;
      nb_col2 = 0;
665
      for (auto iter = database.beginSafe(); iter != database.endSafe(); ++iter) {
666
        const auto& r = iter->row();
667 668
        nb_col1 += r[0].integer();
        nb_col2 += r[1].integer();
669
      }
670 671
      TS_ASSERT(nb_col1 == 8);
      TS_ASSERT(nb_col2 == 8);
672

673 674 675 676 677 678 679
      std::vector< gum::learning::RawDatabaseTable<>::IsMissing > is_miss(
        4, gum::learning::RawDatabaseTable<>::IsMissing::False);
      gum::learning::DBRow< gum::learning::DBCell > row3(3,
                                                         gum::learning::DBCell(4));
      gum::learning::DBRow< gum::learning::DBCell > row4(3,
                                                         gum::learning::DBCell(5));
      database.insertRows({row3, row4}, is_miss);
680 681 682

      nb_col1 = 0;
      nb_col2 = 0;
683
      for (const auto row : database) {
684
        const auto& r = row.row();
685 686
        nb_col1 += r[0].integer();
        nb_col2 += r[1].integer();
687
      }
688 689
      TS_ASSERT(nb_col1 == 17);
      TS_ASSERT(nb_col2 == 17);
690 691 692

      nb_col1 = 0;
      nb_col2 = 0;
693
      for (auto iter = database.beginSafe(); iter != database.endSafe(); ++iter) {
694
        const auto& r = (*iter).row();
695 696
        nb_col1 += r[0].integer();
        nb_col2 += r[1].integer();
697
      }
698 699
      TS_ASSERT(nb_col1 == 17);
      TS_ASSERT(nb_col2 == 17);
700 701
    }

702 703 704 705 706 707 708 709 710 711 712 713 714 715
    void test_ignored_colums() {
      const std::vector< std::string >  empty_miss;
      std::vector< std::string >        names{"v0", "v1", "v2", "v3", "v4", "v5"};
      gum::learning::RawDatabaseTable<> database(empty_miss, names);
      const auto&                       dbnames = database.variableNames();
      const auto&                       content = database.content();
      TS_ASSERT(dbnames[0] == "v0");
      TS_ASSERT(dbnames[1] == "v1");
      TS_ASSERT(dbnames[2] == "v2");
      TS_ASSERT(dbnames[3] == "v3");

      gum::learning::DBRow< gum::learning::DBCell > row(6);
      for (std::size_t i = std::size_t(0); i < 6; ++i) {
        row[i] = gum::learning::DBCell(int(i));
716
      }
717 718

      const auto xmiss = gum::learning::RawDatabaseTable<>::IsMissing::False;
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
      database.insertRow(row, xmiss);
      database.insertRow(row, xmiss);
      TS_ASSERT(database.content().size() == std::size_t(2));
      TS_ASSERT(content[0][0] == gum::learning::DBCell(int(0)));
      TS_ASSERT(content[0][1] == gum::learning::DBCell(int(1)));
      TS_ASSERT(content[0][2] == gum::learning::DBCell(int(2)));
      TS_ASSERT(content[0][3] == gum::learning::DBCell(int(3)));
      TS_ASSERT(content[1][0] == gum::learning::DBCell(int(0)));
      TS_ASSERT(content[1][1] == gum::learning::DBCell(int(1)));
      TS_ASSERT(content[1][2] == gum::learning::DBCell(int(2)));
      TS_ASSERT(content[1][3] == gum::learning::DBCell(int(3)));

      database.ignoreColumn(0, true);
      TS_ASSERT(dbnames.size() == std::size_t(5));
      TS_ASSERT(dbnames[0] == "v1");
      TS_ASSERT(dbnames[1] == "v2");
      TS_ASSERT(dbnames[2] == "v3");
      TS_ASSERT(dbnames[3] == "v4");
      TS_ASSERT(database.content().size() == std::size_t(2));
      TS_ASSERT(database.content()[0].size() == std::size_t(5));
      TS_ASSERT(content[0][0] == gum::learning::DBCell(int(1)));
      TS_ASSERT(content[0][1] == gum::learning::DBCell(int(2)));
      TS_ASSERT(content[0][2] == gum::learning::DBCell(int(3)));
      TS_ASSERT(content[0][3] == gum::learning::DBCell(int(4)));
      TS_ASSERT(content[1][0] == gum::learning::DBCell(int(1)));
      TS_ASSERT(content[1][1] == gum::learning::DBCell(int(2)));
      TS_ASSERT(content[1][2] == gum::learning::DBCell(int(3)));
      TS_ASSERT(content[1][3] == gum::learning::DBCell(int(4)));

      database.ignoreColumn(1, false);
      TS_ASSERT(dbnames.size() == std::size_t(4));
      TS_ASSERT(dbnames[0] == "v1");
      TS_ASSERT(dbnames[1] == "v3");
      TS_ASSERT(dbnames[2] == "v4");
      TS_ASSERT(dbnames[3] == "v5");
      TS_ASSERT(database.content().size() == std::size_t(2));
      TS_ASSERT(database.content()[0].size() == std::size_t(4));
      TS_ASSERT(content[0][0] == gum::learning::DBCell(int(1)));
      TS_ASSERT(content[0][1] == gum::learning::DBCell(int(3)));
      TS_ASSERT(content[0][2] == gum::learning::DBCell(int(4)));
      TS_ASSERT(content[0][3] == gum::learning::DBCell(int(5)));
      TS_ASSERT(content[1][0] == gum::learning::DBCell(int(1)));
      TS_ASSERT(content[1][1] == gum::learning::DBCell(int(3)));
      TS_ASSERT(content[1][2] == gum::learning::DBCell(int(4)));
      TS_ASSERT(content[1][3] == gum::learning::DBCell(int(5)));
764 765

      {
766 767 768 769 770 771 772 773 774
        const auto ignored_vect = database.ignoredColumns();
        TS_ASSERT(ignored_vect[0] == std::size_t(0));
        TS_ASSERT(ignored_vect[1] == std::size_t(2));

        const auto cols = database.inputColumns();
        TS_ASSERT(cols[0] == std::size_t(1));
        TS_ASSERT(cols[1] == std::size_t(3));
        TS_ASSERT(cols[2] == std::size_t(4));
        TS_ASSERT(cols[3] == std::size_t(5));
775
      }
776 777 778

      database.ignoreColumn(12, true);
      database.ignoreColumn(10, true);
779
      {
780 781 782 783 784 785 786 787 788 789 790
        const auto ignored_vect = database.ignoredColumns();
        TS_ASSERT(ignored_vect[0] == std::size_t(0));
        TS_ASSERT(ignored_vect[1] == std::size_t(2));
        TS_ASSERT(ignored_vect[2] == std::size_t(10));
        TS_ASSERT(ignored_vect[3] == std::size_t(12));

        const auto cols = database.inputColumns();
        TS_ASSERT(cols[0] == std::size_t(1));
        TS_ASSERT(cols[1] == std::size_t(3));
        TS_ASSERT(cols[2] == std::size_t(4));
        TS_ASSERT(cols[3] == std::size_t(5));
791
      }
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816


      database.ignoreColumn(3, true);
      TS_ASSERT(dbnames.size() == std::size_t(3));
      TS_ASSERT(dbnames[0] == "v1");
      TS_ASSERT(dbnames[1] == "v4");
      TS_ASSERT(dbnames[2] == "v5");
      TS_ASSERT(database.content().size() == std::size_t(2));
      TS_ASSERT(database.content()[0].size() == std::size_t(3));
      TS_ASSERT(content[0][0] == gum::learning::DBCell(int(1)));
      TS_ASSERT(content[0][1] == gum::learning::DBCell(int(4)));
      TS_ASSERT(content[0][2] == gum::learning::DBCell(int(5)));
      TS_ASSERT(content[1][0] == gum::learning::DBCell(int(1)));
      TS_ASSERT(content[1][1] == gum::learning::DBCell(int(4)));
      TS_ASSERT(content[1][2] == gum::learning::DBCell(int(5)));

      database.ignoreColumn(5, true);
      TS_ASSERT(dbnames.size() == std::size_t(2));
      database.ignoreColumn(1, true);
      TS_ASSERT(dbnames.size() == std::size_t(1));
      TS_ASSERT(content[0][0] == gum::learning::DBCell(int(4)));
      TS_ASSERT(content[1][0] == gum::learning::DBCell(int(4)));
      database.ignoreColumn(4, true);
      TS_ASSERT(dbnames.size() == std::size_t(0));
      TS_ASSERT(database.content().size() == std::size_t(0));
817 818 819
    }


820 821 822 823 824
    void test_missing_vals() {
      std::vector< std::string >        missing{"?", "N/A", "???"};
      gum::learning::RawDatabaseTable<> database(missing);
      TS_ASSERT(database.content().size() == 0);
      TS_ASSERT(database.variableNames().size() == 0);
825

826 827 828 829
      std::vector< std::string > vect{"v0", "v1", "v2", "v3"};
      database.setVariableNames(vect);
      TS_ASSERT(database.variableNames().size() == 4);
      TS_ASSERT(database.nbVariables() == 4);
830

831 832
      std::vector< std::string > row{"L0", "L1", "L2", "L0"};
      database.insertRow(row);
833 834

      row[0] = "?";
835 836
      database.insertRow(row);

837 838
      row[0] = "L0";
      row[1] = "?";
839
      database.insertRow(row);
840 841

      row[2] = "N/A";
842
      database.insertRow(row);
843 844

      row[0] = "???";
845
      database.insertRow(row);
846 847 848 849

      row[0] = "L0";
      row[1] = "L0";
      row[2] = "L0";
850
      database.insertRow(row);
851

852 853 854 855 856 857 858 859
      TS_ASSERT(database.hasMissingValues());

      TS_ASSERT(database.hasMissingValues(0) == false);
      TS_ASSERT(database.hasMissingValues(1) == true);
      TS_ASSERT(database.hasMissingValues(2) == true);
      TS_ASSERT(database.hasMissingValues(3) == true);
      TS_ASSERT(database.hasMissingValues(4) == true);
      TS_ASSERT(database.hasMissingValues(5) == false);
860

861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890
      database.ignoreColumn(1);
      TS_ASSERT(database.hasMissingValues());
      TS_ASSERT(database.hasMissingValues(0) == false);
      TS_ASSERT(database.hasMissingValues(1) == true);
      TS_ASSERT(database.hasMissingValues(2) == false);
      TS_ASSERT(database.hasMissingValues(3) == true);
      TS_ASSERT(database.hasMissingValues(4) == true);
      TS_ASSERT(database.hasMissingValues(5) == false);

      database.ignoreColumn(2);
      TS_ASSERT(database.hasMissingValues());
      TS_ASSERT(database.hasMissingValues(0) == false);
      TS_ASSERT(database.hasMissingValues(1) == true);
      TS_ASSERT(database.hasMissingValues(2) == false);
      TS_ASSERT(database.hasMissingValues(3) == false);
      TS_ASSERT(database.hasMissingValues(4) == true);
      TS_ASSERT(database.hasMissingValues(5) == false);

      database.ignoreColumn(0);
      TS_ASSERT(database.hasMissingValues() == false);
      TS_ASSERT(database.hasMissingValues(0) == false);
      TS_ASSERT(database.hasMissingValues(1) == false);
      TS_ASSERT(database.hasMissingValues(2) == false);
      TS_ASSERT(database.hasMissingValues(3) == false);
      TS_ASSERT(database.hasMissingValues(4) == false);
      TS_ASSERT(database.hasMissingValues(5) == false);

      database.ignoreColumn(3);
      TS_ASSERT(database.hasMissingValues() == false);
    }
891 892


893 894 895 896 897
    private:
    void __create_handler(gum::learning::RawDatabaseTable<>*           database,
                          gum::learning::RawDatabaseTable<>::Handler** handler,
                          int*                                         nb) {
      *handler = new gum::learning::RawDatabaseTable<>::Handler(*database);
898 899

      int x = 0;
900 901
      for (const auto& row : **handler) {
        TS_ASSERT(row.size() == 3);
902 903 904 905 906
        x++;
      }

      *nb = x;
    }
907 908 909 910 911 912

    void __create_handler_safe(
      gum::learning::RawDatabaseTable<>*               database,
      gum::learning::RawDatabaseTable<>::HandlerSafe** handler,
      int*                                             nb) {
      *handler = new gum::learning::RawDatabaseTable<>::HandlerSafe(*database);
913 914

      int x = 0;
915 916
      for (const auto& row : **handler) {
        TS_ASSERT(row.size() == 3);
917 918 919 920
        x++;
      }

      *nb = x;
921
    }
922 923 924
  };

} /* namespace gum_tests */