From 5bfa8321d0294b9c6fa3cfc941cdd3dddf6320e1 Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Thu, 14 Oct 2021 21:26:02 +0200 Subject: [PATCH] Add check dict-set against STL --- tests/check-generic.hpp | 52 +++++++++++++++++++++++++++--- tests/check-uset.cpp | 70 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+), 4 deletions(-) create mode 100644 tests/check-uset.cpp diff --git a/tests/check-generic.hpp b/tests/check-generic.hpp index d970d277..65cc4907 100644 --- a/tests/check-generic.hpp +++ b/tests/check-generic.hpp @@ -130,8 +130,23 @@ void test(int select_test, int index, if (!c1.empty()) { // To rework for dict index = index % c1.size(); +#ifndef HAVE_GET_KEY_THROUGH_FIND M_CALL_SET(M_GET_OPLIST CONT_OPL, b0, *M_CALL_GET_KEY(CONT_OPL, c0, index)); b1 = c1[index]; +#else + BASE_TYPE *p = M_CALL_GET_KEY(CONT_OPL, c0, index); + if (p!= NULL) { + M_CALL_SET(M_GET_OPLIST CONT_OPL, b0, *p); + auto i = c1.find(index); + assert(i != c1.end()); + b1 = *i; + } else { + auto i = c1.find(index); + assert(i == c1.end()); + TYPE_SET_INT(b0, 0); + CLASS_SET_INT(b1, 0); + } +#endif } break; #endif @@ -153,8 +168,18 @@ void test(int select_test, int index, if (!c1.empty()) { index = index % c1.size(); p0 = M_CALL_ERASE_KEY(CONT_OPL, c0, index); +#ifndef HAVE_GET_KEY_THROUGH_FIND c1.erase(c1.begin() + index); p1 = true; +#else + auto i = c1.find(index); + if (i != c1.end()) { + c1.erase(i); + p1 = true; + } else { + p1 = false; + } +#endif } break; #endif @@ -206,10 +231,21 @@ void test(int select_test, int index, // To rework for dict index = (1 + (c1.size() < 1000000)) * (index % ( c1.size() + 1) ); M_CALL_SET(M_GET_OPLIST CONT_OPL, b0, *M_CALL_GET_SET_KEY(CONT_OPL, c0, index)); +#ifndef HAVE_GET_KEY_THROUGH_FIND if ((size_t) index >= c1.size() ) { c1.resize(index+1); } b1 = c1[index]; +#else + { + auto i = c1.find(index); + if (i == c1.end()) { + c1.insert(index); + i = c1.find(index); + } + b1 = *i; + } +#endif break; #endif @@ -217,15 +253,23 @@ void test(int select_test, int index, case 15: M_CALL_IT_FIRST(CONT_OPL, it0, c0); it1 = c1.begin(); - while (it1 != c1.end()) { - assert( !M_CALL_IT_END_P(CONT_OPL, it0)); + while (!M_CALL_IT_END_P(CONT_OPL, it0)) { M_CALL_SET(M_GET_OPLIST CONT_OPL, b0, *M_CALL_IT_CREF(CONT_OPL, it0)); +#ifndef DONT_HAVE_SEQUENCE_IT + assert(it1 != c1.end()); b1 = *it1; + it1++; +#else + it1 = c1.find(b0); + assert(it1 != c1.end()); + b1 = *it1; +#endif CMP_BASE(b0, b1); M_CALL_IT_NEXT(CONT_OPL, it0); - it1++; } - assert(M_CALL_IT_END_P(CONT_OPL, it0)); +#ifndef DONT_HAVE_SEQUENCE_IT + assert(it1 == c1.end()); +#endif break; #endif diff --git a/tests/check-uset.cpp b/tests/check-uset.cpp new file mode 100644 index 00000000..a8f91378 --- /dev/null +++ b/tests/check-uset.cpp @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2021, Patrick Pelissier + * All rights reserved. + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +#include +#include "m-dict.h" + +DICT_SET_DEF(dict_int, int) + +// Transform an integer 'i' into a M*LIB base item 'a' +#define TYPE_SET_INT(a, i) (a) = (i) + +// Container OPLIST +#define CONT_OPL DICT_SET_OPLIST(dict_int, M_OPL_int()) + +// C++ Base class of the item in the container +#define BASE_CLASS int + +// Transform an integer 'i' into a C++ base item 'a' +#define CLASS_SET_INT(a, i) (a) = (i) + +// C++ Container class +#define CONT_CLASS std::unordered_set + +// Compare the M*LIB container a to the C++ container b +#define CMP_CONT(a, b) cmp_cont(a, b) + +// Compate the M*LIB base object to the C++ base object +#define CMP_BASE(a, b) assert( (a) == (b) ) + +void cmp_cont(const dict_int_t a, const std::unordered_set &b) +{ + size_t n = 0; + auto itb = b.begin(); + while (itb != b.end()) { + const int &b1 = *itb; + const int *b0 = dict_int_get(a, b1); + assert(b0 != nullptr); + CMP_BASE(*b0, b1); + itb++; + n++; + } + assert(dict_int_size(a) == n); +} + +#define DEFAULT_NUMBER 1000000 + +#define HAVE_GET_KEY_THROUGH_FIND +#define DONT_HAVE_SEQUENCE_IT + +#define push_back insert +#include "check-generic.hpp"