Simplify & factorize the instances of the check against C++

This commit is contained in:
Patrick Pelissier committed 2022-11-30 23:56:29 +01:00
1 parent f25bb73e58
commit bcd0c46e8b
10 files changed
+41 -91

No files matched your search

+1 -10
View File
@@ -25,27 +25,18 @@
ARRAY_DEF(array_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 ARRAY_OPLIST(array_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::vector<int>
// Compare the M*LIB container a to the C++ container b
#define CMP_CONT(a, b) cmp_cont(a, b)
// Compare the M*LIB base object to the C++ base object
#define CMP_BASE(a, b) assert( (a) == (b) )
void cmp_cont(array_int_t a, std::vector<int> b)
{
size_t s = array_int_size(a);
@@ -53,7 +44,7 @@ void cmp_cont(array_int_t a, std::vector<int> b)
for(size_t i = 0; i < s; i++) {
int *b0 = array_int_get(a, i);
int &b1 = b.at(i);
CMP_BASE(*b0, b1);
assert(*b0 == b1);
}
}
+1 -10
View File
@@ -25,27 +25,18 @@
BPTREE_DEF(tree_int, 5, 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 BPTREE_OPLIST(tree_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::set<int>
// Compare the M*LIB container a to the C++ container b
#define CMP_CONT(a, b) cmp_cont(a, b)
// Compare the M*LIB base object to the C++ base object
#define CMP_BASE(a, b) assert( (a) == (b) )
void cmp_cont(const tree_int_t a, const std::set<int> &b)
{
tree_int_it_t ita;
@@ -55,7 +46,7 @@ void cmp_cont(const tree_int_t a, const std::set<int> &b)
assert(!tree_int_end_p(ita));
const int *b0 = tree_int_cref(ita);
const int &b1 = *itb;
CMP_BASE(*b0, b1);
assert(*b0 == b1);
itb++;
tree_int_next(ita);
}
+1 -10
View File
@@ -25,27 +25,18 @@
DEQUE_DEF(deque_float, float)
// Transform an integer 'i' into a M*LIB base item 'a'
#define TYPE_SET_INT(a, i) (a) = (i)
// Container OPLIST
#define CONT_OPL DEQUE_OPLIST(deque_float, M_OPL_float())
// C++ Base class of the item in the container
#define BASE_CLASS float
// 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::deque<float>
// Compare the M*LIB container a to the C++ container b
#define CMP_CONT(a, b) cmp_cont(a, b)
// Compare the M*LIB base object to the C++ base object
#define CMP_BASE(a, b) assert( (a) == (b) )
void cmp_cont(deque_float_t a, std::deque<float> b)
{
deque_float_it_t ita;
@@ -55,7 +46,7 @@ void cmp_cont(deque_float_t a, std::deque<float> b)
assert(!deque_float_end_p(ita));
const float *b0 = deque_float_cref(ita);
float &b1 = *itb;
CMP_BASE(*b0, b1);
assert(*b0 == b1);
itb++;
deque_float_next(ita);
}
+1 -11
View File
@@ -25,27 +25,18 @@
LIST_DUAL_PUSH_DEF(list_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 LIST_OPLIST(list_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::list<int>
// Compare the M*LIB container a to the C++ container b
#define CMP_CONT(a, b) cmp_cont(a, b)
// Compare the M*LIB base object to the C++ base object
#define CMP_BASE(a, b) assert( (a) == (b) )
void cmp_cont(const list_int_t a, const std::list<int> &b)
{
list_int_it_t ita;
@@ -55,8 +46,7 @@ void cmp_cont(const list_int_t a, const std::list<int> &b)
assert(!list_int_end_p(ita));
const int *b0 = list_int_cref(ita);
const int &b1 = *itb;
//printf("%d VS %d\n", *b0, b1);
CMP_BASE(*b0, b1);
assert(*b0 == b1);
itb++;
list_int_next(ita);
}
+29 -3
View File
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, Patrick Pelissier
* Copyright (c) 2021-2022 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:
@@ -37,9 +37,15 @@
# define CONT_TYPE_(CONT_OPL) M_GET_TYPE CONT_OPL
# define CONT_TYPE CONT_TYPE_(CONT_OPL)
#endif
// Type used for M*LIB container. Default is the same as C++
#ifndef BASE_TYPE
# define BASE_TYPE_(CONT_OPL) M_GET_SUBTYPE CONT_OPL
# define BASE_TYPE BASE_TYPE_(CONT_OPL)
# define BASE_TYPE BASE_CLASS
#endif
// Compare the M*LIB base object to the C++ base object
#ifndef CMP_BASE
# define CMP_BASE(a, b) assert( (a) == (b) )
#endif
/* Get the affectation if needed to transform an integer into C++ class (resp. M*LIB type) */
@@ -58,9 +64,27 @@
# define TYPE_IT_TO_INT(it) *(it)
#endif
/* Global properties of the container */
// Use find to perform GET_KEY for C++
//#define HAVE_GET_KEY_THROUGH_FIND
// Container are nor ordered
//#define DONT_HAVE_SEQUENCE_IT
// associative array container
//#define HAVE_SET_KEY
// Container is in reverse
//#define HAVE_REVERSE_IT
// Have PUSH_FRONT operator
//#define HAVE_PUSH_FRONT
// Have POP_FRONT operator
//#define HAVE_POP_FRONT
/*********************************************************************************/
// Define the container name, extracting it from its oplist
#define CONT_NAME_(CONT_OPL) M_GET_NAME CONT_OPL
#define CONT_NAME CONT_NAME_(CONT_OPL)
// Define the maximum number of tests
#define MAX_TEST 28
#ifdef HAVE_REVERSE_IT
@@ -456,6 +480,7 @@ void test(int select_test, int index,
M_CALL_REVERSE(CONT_OPL, c0);
c1.reverse();
break;
// 21 is above
#endif
#ifdef HAVE_PUSH_FRONT
@@ -513,6 +538,7 @@ void test(int select_test, int index,
// Needs SPLICE ?
default:
M_STATIC_ASSERT( 28 == MAX_TEST, MLIB_INTERNAL_ERROR, "Constant MAX_TEST is no coherent with the effective number of tests");
// Nothing to do
break;
}
+1 -10
View File
@@ -25,27 +25,18 @@
LIST_DEF(list_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 LIST_OPLIST(list_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::forward_list<int>
// Compare the M*LIB container a to the C++ container b
#define CMP_CONT(a, b) cmp_cont(a, b)
// Compare the M*LIB base object to the C++ base object
#define CMP_BASE(a, b) assert( (a) == (b) )
void cmp_cont(list_int_t a, std::forward_list<int> b)
{
list_int_it_t ita;
@@ -55,7 +46,7 @@ void cmp_cont(list_int_t a, std::forward_list<int> b)
assert(!list_int_end_p(ita));
const int *b0 = list_int_cref(ita);
int &b1 = *itb;
CMP_BASE(*b0, b1);
assert(*b0 == b1);
itb++;
list_int_next(ita);
}
-11
View File
@@ -27,9 +27,6 @@
#define CMP_REVERT(a,b) ((a) > (b) ? -1 : (a) < (b))
PRIOQUEUE_DEF(prio_int, int, M_OPEXTEND(M_BASIC_OPLIST, CMP(CMP_REVERT) ))
// Transform an integer 'i' into a M*LIB base item 'a'
#define TYPE_SET_INT(a, i) (a) = (i)
// Container OPLIST (disable iterators & reset)
#define CONT_OPL \
M_OPEXTEND( PRIOQUEUE_OPLIST(prio_int, M_OPL_int()), \
@@ -47,28 +44,20 @@ PRIOQUEUE_DEF(prio_int, int, M_OPEXTEND(M_BASIC_OPLIST, CMP(CMP_REVERT) ))
// 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::priority_queue<int>
// Compare the M*LIB container a to the C++ container b
#define CMP_CONT(a, b) cmp_cont(a, b)
// Compare the M*LIB base object to the C++ base object
#define CMP_BASE(a, b) assert( (a) == (b) )
void cmp_cont(const prio_int_t a, const std::priority_queue<int> &b)
{
std::priority_queue<int> cb = b;
prio_int_t ca;
prio_int_init_set(ca, a);
//printf("ca="); prio_int_out_str(stdout, ca); printf("\n");
while (!cb.empty()) {
int n;
prio_int_pop(&n, ca);
//printf("N=%d TOP=%d\n", n, cb.top());
assert(n == cb.top());
cb.pop();
}
+1 -11
View File
@@ -25,27 +25,18 @@
RBTREE_DEF(tree_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 RBTREE_OPLIST(tree_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::set<int>
// Compare the M*LIB container a to the C++ container b
#define CMP_CONT(a, b) cmp_cont(a, b)
// Compare the M*LIB base object to the C++ base object
#define CMP_BASE(a, b) assert( (a) == (b) )
void cmp_cont(const tree_int_t a, const std::set<int> &b)
{
tree_int_it_t ita;
@@ -55,8 +46,7 @@ void cmp_cont(const tree_int_t a, const std::set<int> &b)
assert(!tree_int_end_p(ita));
const int *b0 = tree_int_cref(ita);
const int &b1 = *itb;
//printf("%d VS %d\n", *b0, b1);
CMP_BASE(*b0, b1);
assert(*b0 == b1);
itb++;
tree_int_next(ita);
}
+5 -5
View File
@@ -30,7 +30,6 @@ DICT_DEF2(dict_int, int, int)
// C++ Base class of the item in the container
#define BASE_CLASS int
#define BASE_TYPE int
// C++ Container class
#define CONT_CLASS std::unordered_map<int,int>
@@ -38,9 +37,7 @@ DICT_DEF2(dict_int, int, int)
// Compare the M*LIB container a to the C++ container b
#define CMP_CONT(a, b) cmp_cont(a, b)
// Compare the M*LIB base object to the C++ base object
#define CMP_BASE(a, b) assert( (a) == (b) )
// Get the value from the iterator
#define CLASS_IT_TO_INT(it) (it)->second
#define TYPE_IT_TO_INT(it) (it)->value
@@ -51,7 +48,7 @@ void cmp_cont(const dict_int_t a, const std::unordered_map<int,int> &b)
while (itb != b.end()) {
const int *b0 = dict_int_get(a, itb->first);
assert(b0 != nullptr);
CMP_BASE(*b0, itb->second);
assert(*b0 == itb->second);
itb++;
n++;
}
@@ -60,8 +57,11 @@ void cmp_cont(const dict_int_t a, const std::unordered_map<int,int> &b)
#define DEFAULT_NUMBER 1000000
// Use find to perform GET_KEY for C++
#define HAVE_GET_KEY_THROUGH_FIND
// Container are nor ordered
#define DONT_HAVE_SEQUENCE_IT
// associative array container
#define HAVE_SET_KEY
#define push_back insert
+1 -10
View File
@@ -25,27 +25,18 @@
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<int>
// Compare the M*LIB container a to the C++ container b
#define CMP_CONT(a, b) cmp_cont(a, b)
// Compare 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<int> &b)
{
size_t n = 0;
@@ -54,7 +45,7 @@ void cmp_cont(const dict_int_t a, const std::unordered_set<int> &b)
const int &b1 = *itb;
const int *b0 = dict_int_get(a, b1);
assert(b0 != nullptr);
CMP_BASE(*b0, b1);
assert(*b0 == b1);
itb++;
n++;
}