Add initial stuff

This commit is contained in:
Patrick Pelissier committed 2017-02-18 18:51:04 +01:00
1 parent 2d8bda0fe0
commit 8e9d20ba1a
45 files changed
+8901

No files matched your search

+63
View File
@@ -0,0 +1,63 @@
CC=gcc -std=c99
CFLAGS=-Wall -W -pedantic -Wshadow -Wpointer-arith -Wcast-qual -Wstrict-prototypes -Wmissing-prototypes -Wswitch-default -Wswitch-enum -Wcast-align -Wpointer-arith -Wbad-function-cast -Wstrict-overflow=5 -Wstrict-prototypes -Winline -Wundef -Wnested-externs -Wcast-qual -Wshadow -Wunreachable-code -Wlogical-op -Wstrict-aliasing=2 -Wredundant-decls -Wold-style-definition -Wno-unused-function
CPPFLAGS=-I..
RM=rm -f
VALGRIND=
EXE=./a.exe
all:
@echo "Nothing to be done."
check: core array list i-list string buffer shared tuple snapshot algo dict rbtree
valgrind:
make check VALGRIND=valgrind
core:
$(CC) $(CFLAGS) $(CPPFLAGS) -g $(COVERAGE_CFLAGS) test-mcore.c -lgmp -o $(EXE) && $(VALGRIND) $(EXE)
array:
$(CC) $(CFLAGS) $(CPPFLAGS) -g $(COVERAGE_CFLAGS) test-marray.c -lgmp -o $(EXE) && $(VALGRIND) $(EXE)
list:
$(CC) $(CFLAGS) $(CPPFLAGS) -g $(COVERAGE_CFLAGS) test-mlist.c -lgmp -o $(EXE) && $(VALGRIND) $(EXE)
i-list:
$(CC) $(CFLAGS) $(CPPFLAGS) -g $(COVERAGE_CFLAGS) test-milist.c -lgmp -o $(EXE) && $(VALGRIND) $(EXE)
string:
$(CC) $(CFLAGS) $(CPPFLAGS) -g $(COVERAGE_CFLAGS) test-mstring.c -lgmp -o $(EXE) && $(VALGRIND) $(EXE)
buffer:
$(CC) $(CFLAGS) $(CPPFLAGS) -g $(COVERAGE_CFLAGS) test-mbuffer.c -lpthread -o $(EXE) && $(VALGRIND) $(EXE)
shared:
$(CC) $(CFLAGS) $(CPPFLAGS) -g $(COVERAGE_CFLAGS) test-mshared.c -lpthread -o $(EXE) && $(VALGRIND) $(EXE)
algo:
$(CC) $(CFLAGS) $(CPPFLAGS) -g $(COVERAGE_CFLAGS) test-malgo.c -lgmp -o $(EXE) && $(VALGRIND) $(EXE)
tuple:
$(CC) $(CFLAGS) $(CPPFLAGS) -g $(COVERAGE_CFLAGS) test-mtuple.c -lgmp -o $(EXE) && $(VALGRIND) $(EXE)
snapshot:
$(CC) $(CFLAGS) $(CPPFLAGS) -g $(COVERAGE_CFLAGS) test-msnapshot.c -lgmp -o $(EXE) && $(VALGRIND) $(EXE)
dict:
$(CC) $(CFLAGS) $(CPPFLAGS) -g $(COVERAGE_CFLAGS) test-mdict.c -lgmp -o $(EXE) && $(VALGRIND) $(EXE)
rbtree:
$(CC) $(CFLAGS) $(CPPFLAGS) -g $(COVERAGE_CFLAGS) test-mrbtree.c -lgmp -o $(EXE) && $(VALGRIND) $(EXE)
bitset:
$(CC) $(CFLAGS) $(CPPFLAGS) -g $(COVERAGE_CFLAGS) test-mbitset.c -lgmp -o $(EXE) && $(VALGRIND) $(EXE)
gene-list:
$(CC) $(CFLAGS) $(CPPFLAGS)-O3 -DNDEBUG -S tgen-mlist.c && less tgen-mlist.s
gene-array:
$(CC) $(CFLAGS) $(CPPFLAGS)-O3 -DNDEBUG -S tgen-marray.c && less tgen-marray.s
clean:
$(RM) $(EXE) *.s *~ *.o ./a.dat
$(RM) -r coverage *.gcda *.gcno all.info test.tar README.html
+18
View File
@@ -0,0 +1,18 @@
hello:salut
README:lisez-moi
dictionary:dictionnaire
vector:vecteur
list:liste
buffer:tableau
circular:circulaire
gnu:gnu is not unix
README:LISEZ-MOI
file:fichier
string:chaine de caractere
reference:reference
const:constante
error:erreur
char:caractere
key:cle
value:valeur
print:afficher
+77
View File
@@ -0,0 +1,77 @@
#include "m-list.h"
#include "m-array.h"
#include "m-string.h"
#include "m-algo.h"
ARRAY_DEF(int, int)
ALGO_DEF(algo_array, ARRAY_OPLIST(int))
LIST_DEF(int, int)
ALGO_DEF(algo_list, LIST_OPLIST(int))
LIST_DEF(string, string_t, STRING_OPLIST)
ALGO_DEF(algo_string, LIST_OPLIST(string, STRING_OPLIST))
static void test_list(void)
{
list_int_t l;
list_int_init(l);
for(int i = 0; i < 100; i++)
list_int_push_back (l, i);
assert( algo_list_contains(l, 62) == true);
assert( algo_list_contains(l, -1) == false);
assert( algo_list_count(l, 1) == 1);
list_int_push_back (l, 17);
assert( algo_list_count(l, 17) == 2);
assert( algo_list_count(l, -1) == 0);
list_int_clear(l);
}
static void test_array(void)
{
array_int_t l;
array_int_init(l);
for(int i = 0; i < 100; i++)
array_int_push_back (l, i);
assert( algo_array_contains(l, 62) == true);
assert( algo_array_contains(l, -1) == false);
assert( algo_array_count(l, 1) == 1);
array_int_push_back (l, 17);
assert( algo_array_count(l, 17) == 2);
assert( algo_array_count(l, -1) == 0);
array_int_clear(l);
}
static void test_string(void)
{
list_string_t l;
list_string_init(l);
string_t s;
string_init_set_str(s, "Hello, World, John");
algo_string_split(l, s, ',');
list_string_pop_back (&s, l);
assert (string_equal_str_p(s, "Hello"));
list_string_pop_back (&s, l);
assert (string_equal_str_p(s, " World"));
list_string_pop_back (&s, l);
assert (string_equal_str_p(s, " John"));
assert (list_string_empty_p(l));
string_clear(s);
list_string_clear(l);
}
int main(void)
{
test_list();
test_array();
test_string();
exit(0);
}
+197
View File
@@ -0,0 +1,197 @@
/*
* Copyright (c) 2017, 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 <stdio.h>
#include <gmp.h>
#include "m-array.h"
ARRAY_DEF(uint, unsigned int)
#define ARRAY_UINT_OPLIST ARRAY_OPLIST(uint)
static void my_mpz_out_str(FILE *f, const mpz_t z)
{
mpz_out_str (f, 10, z);
}
static bool my_mpz_in_str(mpz_t z, FILE *f)
{
return mpz_inp_str (z, f, 10) > 0;
}
static bool my_mpz_equal_p(const mpz_t z1, const mpz_t z2)
{
return mpz_cmp (z1, z2) == 0;
}
#include "m-string.h"
static void my_mpz_str(string_t str, const mpz_t z, bool append)
{
char *s = mpz_get_str(NULL, 10, z);
assert (s != NULL);
if (append) string_cat_str (str, s);
else string_set_str (str, s);
free(s);
}
ARRAY_DEF(mpz, mpz_t,
(INIT(mpz_init), INIT_SET(mpz_init_set), SET(mpz_set), CLEAR(mpz_clear), \
OUT_STR(my_mpz_out_str), IN_STR(my_mpz_in_str), EQUAL(my_mpz_equal_p), \
GET_STR(my_mpz_str) ))
static void test_uint(void)
{
array_uint_t v;
array_uint_init(v);
assert (array_uint_empty_p(v) == true);
array_uint_push_back(v, 2);
assert (array_uint_empty_p(v) == false);
unsigned int x;
array_uint_pop_back(&x, v);
assert( x == 2);
for(unsigned int i = 0; i < 100; i++)
array_uint_push_back(v, i);
unsigned int s = 0;
for(size_t i = 0; i < array_uint_size(v) ; i++)
s += *array_uint_cget(v, i);
assert( s == 100 * 99 /2);
s = 0;
for M_EACH(item, v, ARRAY_UINT_OPLIST) {
s += *item;
}
assert( s == 100 * 99 /2);
array_uint_pop_at (NULL, v, 0);
assert(array_uint_size(v) == 99);
s= 0;
for(size_t i = 0; i < array_uint_size(v) ; i++)
s += *array_uint_cget(v, i);
assert( s == 100 * 99 /2);
unsigned int *p = array_uint_get(v, 1);
*p += 999;
s= 0;
for(size_t i = 0; i < array_uint_size(v) ; i++)
s += *array_uint_cget(v, i);
assert( s == 100 * 99 /2 + 999);
array_uint_swap(v, 2, 3);
assert(*array_uint_cget(v, 2) == 4);
assert(*array_uint_cget(v, 3) == 3);
array_uint_shrink_to_fit(v);
assert(array_uint_capacity(v) == array_uint_size(v));
const unsigned int *src = array_uint_cget(v, 4);
assert (*src == 5);
array_uint_remove_v(v, 1, array_uint_size(v) -1);
assert(array_uint_size(v) == 2);
assert(*array_uint_cget(v, 0) == 1);
assert(*array_uint_cget(v, 1) == 99);
for(unsigned int i = 3; i < 100; i++)
array_uint_push_back(v, i);
array_uint_resize(v, 10);
assert(array_uint_size(v) == 10);
assert(*array_uint_cget(v, 9) == 10);
array_uint_resize(v, 1000);
assert(array_uint_size(v) == 1000);
assert(*array_uint_cget(v, 999) == 0);
array_uint_t v2;
array_uint_init_set(v2,v);
array_uint_clear(v);
assert(array_uint_size(v2) == 1000);
assert(*array_uint_cget(v2, 999) == 0);
array_uint_push_at (v2, 0, 1742);
assert (array_uint_size(v2) == 1001);
assert(*array_uint_cget(v2, 0) == 1742);
assert(*array_uint_cget(v2, 1000) == 0);
array_uint_insert_v (v2, 500, 504);
assert (array_uint_size(v2) == 1005);
assert(*array_uint_cget(v2, 0) == 1742);
assert(*array_uint_cget(v2, 500) == 0);
assert(array_uint_size(v2) == 1005);
assert(*array_uint_cget(v2, 1004) == 0);
array_uint_clear(v2);
}
static void test_mpz(void)
{
array_mpz_t array1, array2;
mpz_t z;
array_mpz_init (array1);
array_mpz_init (array2);
mpz_init (z);
for(int n = 0; n < 1000; n++) {
mpz_set_ui (z, n);
array_mpz_push_back (array1, z);
}
FILE *f = fopen ("a.dat", "wt");
if (!f) abort();
array_mpz_out_str(f, array1);
fclose (f);
f = fopen ("a.dat", "rt");
if (!f) abort();
bool b = array_mpz_in_str (array2, f);
assert (b == true);
assert (array_mpz_equal_p (array1, array2));
fclose(f);
array_mpz_clean(array1);
M_LET(str, STRING_OPLIST) {
array_mpz_get_str(str, array1, false);
assert (string_cmp_str (str, "[]") == 0);
mpz_set_ui (z, 17);
array_mpz_push_back (array1, z);
array_mpz_get_str(str, array1, false);
assert (string_cmp_str (str, "[17]") == 0);
mpz_set_ui (z, 42);
array_mpz_push_back (array1, z);
array_mpz_get_str(str, array1, true);
assert (string_cmp_str (str, "[17][17,42]") == 0);
// NOTE: Not the same order than list...
}
mpz_clear (z);
array_mpz_clear(array2);
array_mpz_clear(array1);
}
int main(void)
{
test_uint();
test_mpz();
}
+115
View File
@@ -0,0 +1,115 @@
/*
* Copyright (c) 2017, 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 <stdio.h>
#include "m-bitset.h"
static void test1(void)
{
bitset_t set;
bitset_init(set);
for(int i = 0; i < 100; i ++) {
bitset_push_back(set, i%2);
assert (bitset_back(set) == (i%2));
}
for(int i = 0; i < 100; i ++) {
assert (bitset_get(set, i) == (i%2));
}
assert(bitset_size(set) == 100);
assert(bitset_empty_p(set) == false);
bitset_t set2;
bitset_init_set(set2, set);
bitset_clean(set);
assert(bitset_size(set) == 0);
assert(bitset_empty_p(set) == true);
assert(bitset_size(set2) == 100);
assert(bitset_empty_p(set2) == false);
bitset_resize(set2, 50);
assert(bitset_size(set2) == 50);
assert(bitset_empty_p(set2) == false);
for(int i = 0; i < 50; i ++) {
assert (bitset_get(set2, i) == (i%2));
}
// Push from 0
bitset_push_at(set2, 0, 1);
assert (bitset_get(set2, 0) == 1);
for(int i = 1; i < 51; i ++) {
assert (bitset_get(set2, i) == ((i-1)%2));
}
// Push from not aligned bit
bitset_push_at(set2, 9, 1);
for(int i = 1; i < 9; i ++) {
assert (bitset_get(set2, i) == ((i-1)%2));
}
assert (bitset_get(set2, 9) == 1);
for(int i = 10; i < 52; i ++) {
assert (bitset_get(set2, i) == ((i)%2));
}
bool b;
bitset_pop_back(NULL, set2);
bitset_pop_back(&b, set2);
assert(b == false);
assert(bitset_size(set2) == 50);
for(int i = 0; i < 200; i++)
bitset_push_back(set, (i%2));
bitset_pop_at(&b, set, 0);
assert (b == false);
assert (bitset_size(set) == 199);
bitset_pop_at(&b, set, 0);
assert (b == true);
assert (bitset_size(set) == 198);
bitset_pop_at(&b, set, 9);
assert (b == true);
assert (bitset_size(set) == 197);
bitset_pop_at(&b, set, 9);
assert (b == false);
assert (bitset_size(set) == 196);
bitset_clean(set);
for(int i = 0; i < 200; i++)
bitset_push_back(set, (i*17547854547UL)&1) ;
bitset_it_t it;
int n = 0;
for(bitset_it(it, set); ! bitset_end_p(it); bitset_next(it)) {
bool v = (n*17547854547UL)&1;
bool c = bitset_ref(it);
assert (v == c);
n++;
}
bitset_clear(set);
bitset_clear(set2);
}
int main(void)
{
test1();
exit(0);
}
+267
View File
@@ -0,0 +1,267 @@
/*
* Copyright (c) 2017, 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 "m-buffer.h"
// Define a fixed queue of unsigned int
BUFFER_DEF(uint, unsigned int, 10, BUFFER_QUEUE|BUFFER_BLOCKING)
// Define a variable stack of float
BUFFER_DEF(floats, float, 0, BUFFER_STACK|BUFFER_BLOCKING)
// Define a fixed stack of char
BUFFER_DEF(char, char, 10, BUFFER_STACK|BUFFER_UNBLOCKING)
// Define a fixed queue of long long
BUFFER_DEF(llong, long long, 16, BUFFER_QUEUE|BUFFER_THREAD_UNSAFE|BUFFER_UNBLOCKING)
#include <gmp.h>
BUFFER_DEF(mpz, mpz_t, 32, BUFFER_QUEUE, (INIT(mpz_init), INIT_SET(mpz_init_set), SET(mpz_set), CLEAR(mpz_clear)))
buffer_uint_t g_buff;
static void *conso(void *arg)
{
unsigned int j;
assert (arg == NULL);
for(int i = 0; i < 1000;i++) {
buffer_uint_pop(&j, g_buff);
assert (j < 1000);
}
return NULL;
}
static void *prod(void *arg)
{
assert (arg == NULL);
for(unsigned int i = 0; i < 1000;i++)
buffer_uint_push(g_buff, i);
return NULL;
}
static void test_global(void)
{
M_THREAD_T idx_p[100];
M_THREAD_T idx_c[100];
buffer_uint_init(g_buff, 10);
for(int i = 0; i < 100; i++) {
M_THREAD_CREATE (idx_p[i], conso, NULL);
M_THREAD_CREATE (idx_c[i], prod, NULL);
}
for(int i = 0; i < 100;i++) {
M_THREAD_JOIN(idx_p[i]);
M_THREAD_JOIN(idx_c[i]);
}
buffer_uint_clear(g_buff);
}
static void test_stack(void)
{
buffer_floats_t buff;
buffer_floats_init(buff, 100);
for(unsigned int i = 0; i < 100; i++) {
float f = i;
buffer_floats_push(buff, f);
assert (buffer_floats_empty_p(buff) == false);
}
assert (buffer_floats_full_p(buff) == true);
for(unsigned int i = 0; i < 100; i++) {
float j;
buffer_floats_pop(&j, buff);
assert(j == 99-i);
assert (buffer_floats_full_p(buff) == false);
}
assert (buffer_floats_empty_p(buff) == true);
buffer_floats_clear(buff);
}
static void test_stack2(void)
{
buffer_char_t buff;
bool b;
char c;
buffer_char_init(buff, 10);
for(int i = 0; i < 10; i++) {
c = i;
b = buffer_char_push(buff, c);
assert (b == true);
assert (buffer_char_empty_p(buff) == false);
}
b = buffer_char_push(buff, c);
assert (b == false);
assert (buffer_char_full_p(buff) == true);
for(int i = 0; i < 10; i++) {
buffer_char_pop(&c, buff);
assert(c == 9-i);
assert (buffer_char_full_p(buff) == false);
}
b = buffer_char_pop(&c, buff);
assert (b == false);
assert (buffer_char_empty_p(buff) == true);
buffer_char_clear(buff);
}
static void test_no_thread(void)
{
buffer_llong_t buff;
bool b;
long long c;
buffer_llong_init(buff, 16);
for(int i = 0; i < 16; i++) {
c = i;
b = buffer_llong_push(buff, c);
assert (b == true);
assert (buffer_llong_empty_p(buff) == false);
}
b = buffer_llong_push(buff, c);
assert (b == false);
assert (buffer_llong_full_p(buff) == true);
for(int i = 0; i < 16; i++) {
buffer_llong_pop(&c, buff);
assert(c == i);
assert (buffer_llong_full_p(buff) == false);
}
b = buffer_llong_pop(&c, buff);
assert (b == false);
assert (buffer_llong_empty_p(buff) == true);
buffer_llong_clear(buff);
}
/* Test intrusive shared pointer + buffer */
#include "m-i-shared.h"
// Tiny test structure
typedef struct test_s {
char buffer[52];
char bigbuffer[1000000];
ISHARED_PTR_INTERFACE(itest, struct test_s);
} test_t;
static void test_init(test_t *p) { memset(p->buffer, 0x00, 52); }
static void test_clear(test_t *p) { memset(p->buffer, 0xFF, 52); }
ISHARED_PTR_DEF(itest, test_t,
(INIT(test_init M_IPTR), CLEAR(test_clear M_IPTR), DEL(free)))
static test_t *test_new(void)
{
test_t *p = malloc (sizeof (test_t));
assert (p != NULL);
test_init (p);
return ishared_itest_init(p);
}
BUFFER_DEF(itest, test_t *, 16, BUFFER_PUSH_INIT_POP_MOVE,
ISHARED_PTR_OPLIST(itest))
static buffer_itest_t comm1;
static buffer_itest_t comm2;
static void *test_conso1(void *arg)
{
test_t *p;
assert (arg == NULL);
for(int i = 0; i < 10;i++) {
buffer_itest_pop(&p, comm1);
for(int j = 0; j < 52; j++)
assert (p->buffer[j] == (char) ((j * j * 17) + j * 42 + 1));
ishared_itest_release(p);
}
return NULL;
}
static void *test_conso2(void *arg)
{
test_t *p;
assert (arg == NULL);
for(int i = 0; i < 10;i++) {
buffer_itest_pop(&p, comm2);
for(int j = 0; j < 52; j++)
assert (p->buffer[j] == (char) ((j * j * 17) + j * 42 + 1));
ishared_itest_release(p);
}
return NULL;
}
static void *test_prod(void *arg)
{
assert (arg == NULL);
for(unsigned int i = 0; i < 10;i++) {
test_t *p = test_new();
for(int j = 0; j < 52; j++)
p->buffer[j] = (j * j * 17) + j * 42 + 1;
buffer_itest_push(comm1, p);
buffer_itest_push(comm2, p);
ishared_itest_release(p);
}
return NULL;
}
static void test_global_ishared(void)
{
M_THREAD_T idx_p[100];
M_THREAD_T idx_c1[100];
M_THREAD_T idx_c2[100];
buffer_itest_init(comm1, 16);
buffer_itest_init(comm2, 16);
for(int i = 0; i < 100; i++) {
M_THREAD_CREATE (idx_c1[i], test_conso1, NULL);
M_THREAD_CREATE (idx_c2[i], test_conso2, NULL);
M_THREAD_CREATE (idx_p[i], test_prod, NULL);
}
for(int i = 0; i < 100;i++) {
M_THREAD_JOIN(idx_p[i]);
M_THREAD_JOIN(idx_c1[i]);
M_THREAD_JOIN(idx_c2[i]);
}
buffer_itest_clear(comm1);
buffer_itest_clear(comm2);
}
int main(void)
{
unsigned int x, y;
buffer_uint_t v;
buffer_uint_init(v, 10);
assert (buffer_uint_empty_p(v) == true);
assert (buffer_uint_full_p(v) == false);
x = 10;
buffer_uint_push (v, x);
assert (buffer_uint_empty_p(v) == false);
assert (buffer_uint_full_p(v) == false);
buffer_uint_pop (&y, v);
assert (buffer_uint_empty_p(v) == true);
assert (buffer_uint_full_p(v) == false);
assert (y == 10);
buffer_uint_clear(v);
test_global();
test_stack();
test_stack2();
test_no_thread();
test_global_ishared();
}
+151
View File
@@ -0,0 +1,151 @@
/*
* Copyright (c) 2017, 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 <stdbool.h>
#include <assert.h>
#include "m-core.h"
static void test_cat(void)
{
#define AandB true
#define AandBandC true
assert (CAT(A,andB));
assert (CAT3(A,and,B));
assert (CAT4(A,and,B,andC));
}
static void test_power2(void)
{
assert (M_POWEROF2_P(1));
assert (M_POWEROF2_P(2));
assert (!M_POWEROF2_P(3));
assert (M_POWEROF2_P(4));
assert (!M_POWEROF2_P(5));
assert (!M_POWEROF2_P(6));
assert (!M_POWEROF2_P(7));
assert (M_POWEROF2_P(8));
}
static void test_incdec(void)
{
assert (M_INC(0) == 1);
assert (M_INC(1) == 2);
assert (M_INC(2) == 3);
assert (M_INC(3) == 4);
assert (M_INC(4) == 5);
assert (M_INC(5) == 6);
assert (M_INC(6) == 7);
assert (M_INC(7) == 8);
assert (M_INC(8) == 9);
assert (M_INC(9) == 10);
assert (M_INC(10) == 11);
assert (M_INC(11) == 12);
assert (M_INC(12) == 13);
assert (M_INC(13) == 14);
assert (M_INC(14) == 15);
assert (M_INC(15) == 16);
assert (M_INC(16) == 17);
assert (M_INC(17) == 18);
assert (M_INC(18) == 19);
assert (M_INC(19) == 20);
assert (M_INC(20) == 21);
assert (M_INC(21) == 22);
assert (M_INC(22) == 23);
assert (M_INC(23) == 24);
assert (M_INC(24) == 25);
assert (M_INC(25) == 26);
assert (M_INC(26) == 27);
assert (M_INC(27) == 28);
assert (M_INC(28) == 29);
assert ((0) == M_DEC(1));
assert ((1) == M_DEC(2));
assert ((2) == M_DEC(3));
assert ((3) == M_DEC(4));
assert ((4) == M_DEC(5));
assert ((5) == M_DEC(6));
assert ((6) == M_DEC(7));
assert ((7) == M_DEC(8));
assert ((8) == M_DEC(9));
assert ((9) == M_DEC(10));
assert ((10) == M_DEC(11));
assert ((11) == M_DEC(12));
assert ((12) == M_DEC(13));
assert ((13) == M_DEC(14));
assert ((14) == M_DEC(15));
assert ((15) == M_DEC(16));
assert ((16) == M_DEC(17));
assert ((17) == M_DEC(18));
assert ((18) == M_DEC(19));
assert ((19) == M_DEC(20));
assert ((20) == M_DEC(21));
assert ((21) == M_DEC(22));
assert ((22) == M_DEC(23));
assert ((23) == M_DEC(24));
assert ((24) == M_DEC(25));
assert ((25) == M_DEC(26));
assert ((26) == M_DEC(27));
assert ((27) == M_DEC(28));
assert ((28) == M_DEC(29));
assert( M_ADD(2,5) == M_SUB(10,3));
assert( M_ADD(5,15) == M_SUB(25,5));
}
static void test_return(void)
{
assert (M_RET_ARG1(1, 0));
assert (M_RET_ARG2(0, 1, 0));
assert (M_RET_ARG3(0, 0, 1, 0));
assert (M_RET_ARG27(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0));
}
static void test_invert(void)
{
assert (M_RET_ARG1 M_INVERT(0,1));
assert (M_RET_ARG1 M_INVERT(0,0,1));
assert (M_RET_ARG1 M_INVERT(0,0,0,1));
assert (M_RET_ARG1 M_INVERT(0,0,0,0,1));
}
static void test_min(void)
{
assert (M_MIN(1,2) == 1);
assert (M_MIN(1,-2) == -2);
assert (M_MAX(1,2) == 2);
assert (M_MAX(1,-2) == 1);
}
int main(void)
{
test_cat();
test_power2();
test_incdec();
test_return();
test_invert();
test_min();
return 0;
}
+183
View File
@@ -0,0 +1,183 @@
/*
* Copyright (c) 2017, 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 <limits.h>
#include <time.h>
#include <stdint.h>
#include "m-dict.h"
#include "m-array.h"
#include "m-string.h"
DICT_DEF2(str, string_t, STRING_OPLIST, string_t, STRING_OPLIST)
ARRAY_DEF(string, string_t, STRING_OPLIST)
array_string_t v;
static void init_data(int data_size)
{
uint32_t x = 11;
string_t s;
string_init (s);
printf("--- generating data\n");
for (int i = 0; i < data_size; ++i) {
int j = (unsigned)(data_size * ((double)x / UINT_MAX) / 4) * 271828183u;
string_printf(s, "%x", j);
array_string_push_back (v, s);
x = 1664525L * x + 1013904223L;
}
string_clear(s);
printf("--- done!\n");
}
static void clear_data(void)
{
array_string_clear(v);
}
static void test_data(void)
{
dict_str_t dict;
int data_size = (int) array_string_size(v);
dict_str_init (dict);
for (int i = 0; i < data_size; ++i) {
dict_str_set_at(dict, *array_string_cget(v, i), *array_string_cget(v, (i+1) % data_size));
}
assert(dict_str_size(dict) == 1227176);
dict_str_clear (dict);
}
static void timing(void (*f)(void))
{
clock_t t = clock();
(*f)();
printf("timing %.3lf sec\n", (double)(clock() - t) / CLOCKS_PER_SEC);
}
static void check_io(void)
{
M_LET(str, STRING_OPLIST)
M_LET(dict, DICT_OPLIST(str)) {
dict_str_get_str(str, dict, false);
assert (string_cmp_str (str, "{}") == 0);
dict_str_set_at (dict, STRING_CTE("LICENCE"), STRING_CTE("BSD3"));
dict_str_get_str(str, dict, false);
assert (string_cmp_str (str, "{\"LICENCE\":\"BSD3\"}") == 0);
dict_str_set_at (dict, STRING_CTE("AUTHOR"), STRING_CTE("PP"));
dict_str_get_str(str, dict, false);
assert (string_cmp_str (str, "{\"AUTHOR\":\"PP\",\"LICENCE\":\"BSD3\"}") == 0);
FILE *f = fopen ("a.dat", "wt");
if (!f) abort();
dict_str_out_str(f, dict);
fclose (f);
f = fopen ("a.dat", "rt");
if (!f) abort();
M_LET(dict2, DICT_OPLIST(str)) {
bool b = dict_str_in_str(dict2, f);
assert (b == true);
/* assert (dict_str_equal_p (dict, dict2)); */
}
fclose (f);
}
}
int main(void)
{
dict_str_t dict;
string_t key, ref;
dict_str_init(dict);
string_init(key);
string_init(ref);
FILE *f = fopen("dict.txt", "rt");
if (!f) abort();
dict_str_set_at(dict, STRING_CTE("LICENCE"), STRING_CTE("BSD3"));
while (!feof(f) && !ferror(f)) {
string_fgets(key, f, STRING_READ_PURE_LINE);
size_t idx = string_search_char(key, ':');
string_set(ref, key);
string_right(ref, idx+1);
string_left(key, idx);
dict_str_set_at (dict, key, ref);
}
fclose(f);
assert(dict_str_size(dict) == 19);
f = fopen("dict.txt", "rt");
if (!f) abort();
while (!feof(f) && !ferror(f)) {
string_fgets(key, f, STRING_READ_PURE_LINE);
size_t idx = string_search_char(key, ':');
string_set(ref, key);
string_right(ref, idx+1);
string_left(key, idx);
string_t *ptr = dict_str_get (dict, key);
assert(ptr != NULL);
assert(string_equal_p (*ptr, ref) || string_cmp_str(ref, "lisez-moi") == 0);
}
fclose(f);
dict_str_set_at(dict, STRING_CTE("LICENCE"), STRING_CTE("BSD3"));
assert(dict_str_size(dict) == 19);
dict_str_set_at(dict, STRING_CTE("LICENCE"), STRING_CTE("BSD3"));
assert(dict_str_size(dict) == 19);
size_t s = 0;
bool check1 = false, check2= false;
for M_EACH(r, dict, DICT_OPLIST(str)) {
if (string_cmp_str((*r)->key, "README") == 0)
check1 = true;
if (string_cmp_str((*r)->value, "BSD3") == 0)
check2 = true;
//printf ("key=%s value=%s\n", string_get_cstr((*r)->key), string_get_cstr((*r)->value));
s++;
}
assert(s == 19);
assert(check1);
assert(check2);
string_clear(key);
string_clear(ref);
dict_str_clear(dict);
check_io();
init_data(5000000);
timing(test_data);
clear_data();
return 0;
}
/*
[ key1: value1,
key2: value2,
key3: value3]
a;b;"c;e";d;e;f
TOKEN:
* separator ; : , AND .
* ignore ' ' '\n'
* remove blanck
* handle " or ' or ( ) or [ ]
* recursive
*/
+85
View File
@@ -0,0 +1,85 @@
/*
* MLIB - TEST module
*
* Copyright (c) 2017, 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 <stdio.h>
#include "m-i-list.h"
typedef struct test_s {
int n;
ILIST_INTERFACE (tname, struct test_s);
} test_t;
ILIST_DEF(tname, test_t)
static void test(void)
{
test_t x1, x2, x3;
ilist_tname_t list;
x1.n = 1;
x2.n = 2;
x3.n = 3;
ilist_tname_init(list);
assert (ilist_tname_size (list) == 0);
ilist_tname_push_back (list, &x3);
assert (ilist_tname_size (list) == 1);
ilist_tname_push_front (list, &x1);
assert (ilist_tname_size (list) == 2);
ilist_tname_push_after (&x1, &x2);
assert (ilist_tname_size (list) == 3);
assert (ilist_tname_front (list)->n == 1);
assert (ilist_tname_back (list)->n == 3);
int n = 1;
for M_EACH(item, list, ILIST_OPLIST(tname)) {
assert (n == item->n);
n++;
}
for M_EACH(item, list, ILIST_OPLIST(tname)) {
if (item->n == 2)
ilist_tname_unlink(item);
}
assert (ilist_tname_size(list) == 2);
assert (&x1 == ilist_tname_pop_front(list));
assert (ilist_tname_size(list) == 1);
assert (&x3 == ilist_tname_pop_back(list));
assert (ilist_tname_size(list) == 0);
assert (ilist_tname_empty_p(list));
for M_EACH(item, list, ILIST_OPLIST(tname)) {
(void)item;
assert(false);
}
ilist_tname_clear(list);
}
int main(void)
{
test();
exit(0);
}
+229
View File
@@ -0,0 +1,229 @@
/*
* Copyright (c) 2017, 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 <stdio.h>
#include <gmp.h> // For testing purpose only.
#include "m-list.h"
LIST_DEF(uint, unsigned int)
#define LIST_UINT_OPLIST LIST_OPLIST(uint)
static void my_mpz_out_str(FILE *f, const mpz_t z)
{
mpz_out_str (f, 10, z);
}
static bool my_mpz_in_str(mpz_t z, FILE *f)
{
return mpz_inp_str (z, f, 10) > 0;
}
static bool my_mpz_equal_p(const mpz_t z1, const mpz_t z2)
{
return mpz_cmp (z1, z2) == 0;
}
#include "m-string.h"
static void my_mpz_str(string_t str, const mpz_t z, bool append)
{
char *s = mpz_get_str(NULL, 10, z);
if (append) string_cat_str (str, s);
else string_set_str (str, s);
free(s);
}
LIST_DEF(mpz, mpz_t, (INIT(mpz_init), INIT_SET(mpz_init_set), SET(mpz_set), CLEAR(mpz_clear), \
OUT_STR(my_mpz_out_str), IN_STR(my_mpz_in_str), EQUAL(my_mpz_equal_p), \
GET_STR(my_mpz_str)))
static void test_uint(void)
{
list_uint_t v;
list_uint_init(v);
assert (list_uint_empty_p(v) == true);
list_uint_push_back(v, 2);
assert (list_uint_empty_p(v) == false);
unsigned int x ;
list_uint_pop_back(&x, v);
assert( x == 2);
assert (list_uint_empty_p(v) == true);
assert (list_uint_size(v) == 0);
for(unsigned int i = 0; i < 1000; i ++) {
list_uint_push_back(v, i);
}
assert (list_uint_size(v) == 1000);
assert (*list_uint_cget(v, 500) == 500);
assert (*list_uint_cget(v, 999) == 999);
list_it_uint_t u;
unsigned int s = 0;
for(list_uint_it(u, v); !list_uint_end_p(u); list_uint_next(u)) {
s += *list_uint_cref(u);
}
assert (s == 1000*999/2);
s = 0;
for M_EACH(item, v, LIST_UINT_OPLIST) {
s += *item;
}
assert (s == 1000*999/2);
list_uint_insert(v, u, 1996); // Insert in back since u is empty
assert (list_uint_size(v) == 1001);
list_uint_it(u, v);
list_uint_insert(v, u, 1997); // Insert in second place
assert (list_uint_size(v) == 1002);
list_uint_pop_back(&x, v);
assert (x == 1996);
list_uint_pop_back(&x, v);
assert (x == 1997);
assert (list_uint_size(v) == 1000);
list_uint_push_back (v, x);
list_uint_pop_back (NULL, v);
list_uint_t v2;
list_it_uint_t u2;
list_uint_init(v2);
list_uint_set(v2,v);
for(list_uint_it(u, v), list_uint_it(u2, v2); !list_uint_end_p(u); list_uint_next(u), list_uint_next(u2)) {
assert(list_uint_end_p(u2) == false);
assert(*list_uint_cref(u) == *list_uint_cref(u2));
}
list_uint_it(u2, v2); //v2 is the first el
assert (*list_uint_cref(u2) == 999);
list_uint_next(u2); //v2 is the second el
assert (*list_uint_cref(u2) == 998);
list_uint_remove(v2, u2);// the second el is deleted
list_uint_it(u2, v2); //v2 is the first el
assert (*list_uint_cref(u2) == 999);
list_uint_next(u2); //v2 is the second el
assert (*list_uint_cref(u2) == 997);
list_uint_it(u2, v2); //v2 is the first el
list_uint_remove(v2, u2);// the second el is deleted
list_uint_it(u2, v2); //v2 is the first el
assert (*list_uint_cref(u2) == 997);
list_uint_clean(v);
assert (list_uint_empty_p(v) == true);
assert (list_uint_empty_p(v2) == false);
assert (list_uint_size(v2) == 998);
list_uint_set (v, v2);
list_uint_slice (v, v2);
assert (list_uint_empty_p(v) == false);
assert (list_uint_empty_p(v2) == true);
assert (list_uint_size(v) == 998*2);
list_uint_reverse(v2);
assert (list_uint_empty_p(v2) == true);
list_uint_push_back(v2, 12);
list_uint_push_back(v2, 17);
list_uint_reverse(v2);
list_uint_pop_back(&x, v2);
assert (x == 12);
list_uint_pop_back(&x, v2);
assert (x == 17);
assert (list_uint_empty_p(v2) == true);
list_uint_clear(v);
list_uint_clear(v2);
}
static void test_mpz(void)
{
list_mpz_t v;
mpz_t z, x, s;
mpz_init (z);
mpz_init (x);
mpz_init (s);
list_mpz_init(v);
assert (list_mpz_empty_p(v) == true);
mpz_set_ui (z, 2);
list_mpz_push_back(v, z);
assert (list_mpz_empty_p(v) == false);
list_mpz_pop_back(&x, v);
assert( mpz_cmp (x, z) == 0);
assert (list_mpz_empty_p(v) == true);
assert (list_mpz_size(v) == 0);
for(unsigned int i = 0; i < 1000; i ++) {
mpz_set_ui (z, i);
list_mpz_push_back(v, z);
}
assert (list_mpz_size(v) == 1000);
list_it_mpz_t u;
mpz_set_ui (s, 0);
for(list_mpz_it(u, v); !list_mpz_end_p(u); list_mpz_next(u)) {
mpz_add (s, s, *list_mpz_cref(u));
}
assert (mpz_get_ui(s) == 1000*999/2);
FILE *f = fopen ("a.dat", "wt");
if (!f) abort();
list_mpz_out_str(f, v);
fclose (f);
list_mpz_t list2;
list_mpz_init(list2);
f = fopen ("a.dat", "rt");
bool b = list_mpz_in_str (list2, f);
assert (b == true);
fclose(f);
assert (list_mpz_equal_p (v, list2));
list_mpz_clear(list2);
list_mpz_clean(v);
M_LET(str, STRING_OPLIST) {
list_mpz_get_str(str, v, false);
assert (string_cmp_str (str, "[]") == 0);
mpz_set_ui (z, 17);
list_mpz_push_back (v, z);
list_mpz_get_str(str, v, false);
assert (string_cmp_str (str, "[17]") == 0);
mpz_set_ui (z, 42);
list_mpz_push_back (v, z);
list_mpz_get_str(str, v, true);
assert (string_cmp_str (str, "[17][42,17]") == 0);
}
list_mpz_clear(v);
mpz_clear (z);
mpz_clear (x);
mpz_clear (s);
}
int main(void)
{
test_uint();
test_mpz();
}
+104
View File
@@ -0,0 +1,104 @@
/*
* Copyright (c) 2017, 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 <stdio.h>
#include <gmp.h> // For testing purpose only.
#include "m-rbtree.h"
RBTREE_DEF(uint, unsigned int)
#define UINT_OPLIST RBTREE_OPLIST(uint)
RBTREE_DEF(float, float)
#define FLOAT_OP RBTREE_OPLIST(float)
static void test_uint(void)
{
rbtree_uint_t tree, tree2;
rbtree_uint_init(tree);
assert (rbtree_uint_empty_p(tree));
rbtree_uint_init_set(tree2, tree);
assert (rbtree_uint_empty_p(tree2));
rbtree_uint_clear(tree);
const unsigned int max = 1001;
for(unsigned int num = 1; num < max; num++) {
rbtree_uint_init(tree);
for(unsigned int i = 0; i < num; i++)
rbtree_uint_push(tree, i);
assert(rbtree_uint_size(tree) == num);
assert(rbtree_uint_empty_p(tree) == false);
unsigned int count = 0;
for M_EACH(item, tree, UINT_OPLIST) {
assert (count == *item);
count ++;
}
assert (*rbtree_uint_min(tree) == 0);
assert (*rbtree_uint_max(tree) == num-1);
assert (count == num);
rbtree_uint_set(tree2, tree);
unsigned int *ptr = rbtree_uint_get(tree2, num/2);
assert (ptr != NULL);
assert (*ptr == num/2);
rbtree_uint_clear(tree);
bool b = rbtree_uint_pop(NULL, tree2, num+1);
assert (b == false);
for(unsigned int i = 0; i < num; i+=3) {
b = rbtree_uint_pop(NULL, tree2, i);
assert(b);
ptr = rbtree_uint_get(tree2, i);
assert (ptr == NULL);
}
for(unsigned int i = 1; i < num; i+=3) {
b = rbtree_uint_pop(NULL, tree2, i);
assert(b);
ptr = rbtree_uint_get(tree2, i);
assert (ptr == NULL);
}
}
rbtree_uint_clear(tree2);
}
static void test_float(void)
{
M_LET(tree, FLOAT_OP) {
for(float f = -17.42; f < 17.42; f+=0.01)
rbtree_float_push (tree, f);
bool p = false;
float g;
for M_EACH(item, tree, FLOAT_OP) {
if (p) {
assert (g < *item);
}
g = *item;
p = true;
}
}
}
int main(void)
{
test_uint();
test_float();
return 0;
}
+58
View File
@@ -0,0 +1,58 @@
#include "gmp.h"
#include "m-array.h"
#include "m-list.h"
#include "m-register.h"
ARRAY_DEF (mpz, mpz_t, (INIT(mpz_init), CLEAR(mpz_clear), SET(mpz_set), INIT_SET(mpz_init_set)))
LIST_DEF (uint, unsigned int, M_DEFAULT_OPLIST)
// Register new class: mpz_t
#define TYPE mpz_t
#define OPLIST (INIT(mpz_init), CLEAR(mpz_clear), SET(mpz_set), INIT_SET(mpz_init_set))
#include M_REGISTER_TYPE
void mpq_init_set(mpq_t q, const mpq_t p)
{
mpq_init(q);
mpq_set(q, p);
}
// Register new class: mpq_t
#define TYPE mpq_t
#define OPLIST (INIT(mpq_init), CLEAR(mpq_clear), SET(mpq_set), INIT_SET(mpq_init_set))
#include M_REGISTER_TYPE
#define TYPE array_mpz_t
#define OPLIST ARRAY_OPLIST(mpz)
//#include M_REGISTER_TYPE
//#define TYPE list_uint_t
//#define OPLIST LIST_OPLIST(unsigned int)
//#include M_REGISTER_TYPE
bool is42(array_mpz_t array)
{
bool retval = false;
mpz_t *z = new(mpz_t);
mpz_set_ui (*z, 42);
for_each (item, array) {
if (equal(*item, *z)) {
retval = true;
break;
}
}
delete(z);
return retval;
}
void example(void)
{
list_uint_t list;
init(list);
push(list, 42);
push(list, 17);
for_each (item, list) {
print(*item, "\n");
}
clear(list);
}
+59
View File
@@ -0,0 +1,59 @@
#include <assert.h>
#include "m-shared.h"
SHARED_PTR_DEF(int, int)
#include <gmp.h>
SHARED_PTR_DEF(mpz, mpz_t, (CLEAR(mpz_clear)))
static int f(const shared_int_t p)
{
return *shared_int_cref(p);
}
int main(void)
{
shared_int_t p1, p2;
shared_int_init2(p1, malloc(sizeof(int)));
assert(shared_int_ref(p1) != NULL);
*shared_int_ref(p1) = 12;
shared_int_init_set(p2, p1);
assert(f(p2) == 12);
shared_int_release(p1);
assert(shared_int_NULL_p(p1));
assert(f(p2) == 12);
shared_int_set(p1, p2);
shared_int_release(p2);
assert(shared_int_NULL_p(p2));
assert(f(p1) == 12);
shared_int_set(p2, p1);
assert(f(p2) == 12);
shared_int_release(p2);
shared_int_release(p1);
assert(shared_int_NULL_p(p1));
assert(shared_int_NULL_p(p2));
shared_int_clear(p2);
shared_int_clear(p1);
shared_int_init2(p1, NULL);
assert(shared_int_NULL_p(p1));
shared_int_clear(p1);
shared_int_init(p1);
assert(shared_int_NULL_p(p1));
shared_int_clear(p1);
return 0;
}
+74
View File
@@ -0,0 +1,74 @@
/*
* Copyright (c) 2017, 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 <gmp.h> // For testing purpose only.
#include "m-snapshot.h"
SNAPSHOT_DEF(uint, unsigned int)
SNAPSHOT_DEF(mpz, mpz_t,
(INIT(mpz_init), INIT_SET(mpz_init_set), SET(mpz_set), CLEAR(mpz_clear)))
static void test_uint(void)
{
unsigned int *p_w;
const unsigned int *p_r;
snapshot_uint_t t;
snapshot_uint_init(t);
p_r = snapshot_uint_look(t);
assert (p_r == snapshot_uint_get_consummed(t));
assert(*p_r == 0);
assert(snapshot_uint_look(t) == p_r);
p_w = snapshot_uint_take(t);
assert (p_w != p_r);
*p_w = 1;
assert (*p_r == 0);
assert (snapshot_uint_look(t) != p_r);
assert (*snapshot_uint_look(t) == 0);
p_r = snapshot_uint_look(t);
p_w = snapshot_uint_take(t);
*p_w = 2;
assert (*snapshot_uint_look(t) == 1);
p_w = snapshot_uint_take(t);
assert (p_w == snapshot_uint_get_produced(t));
*p_w = 3;
p_w = snapshot_uint_take(t);
*p_w = 4;
assert (*snapshot_uint_look(t) == 3);
snapshot_uint_t t2;
snapshot_uint_init_set (t2, t);
snapshot_uint_clear(t);
assert (*snapshot_uint_look(t) == 3);
snapshot_uint_init(t);
snapshot_uint_set (t, t2);
assert (*snapshot_uint_look(t) == 3);
}
int main(void)
{
test_uint();
}
+195
View File
@@ -0,0 +1,195 @@
/*
* Copyright (c) 2017, 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 "m-string.h"
int main(void)
{
string_t s1;
STRING_DECL_INIT (s2);
size_t i, j;
bool b;
string_init (s1);
string_set_str (s1, "Hello, world!");
assert (string_get_length(s1) == 13);
assert (string_get_char(s1, 1) == 'e');
string_clean(s1);
assert (string_get_length(s1) == 0);
assert (string_empty_p(s1) == true);
assert (strcmp(string_get_cstr(s1), "") == 0);
string_set_strn(s1, "Hello, world!", 14);
assert (strcmp(string_get_cstr(s1), "Hello, world!") == 0);
string_clean(s1);
string_set_strn(s1, "Hello, world!", 5);
assert (strcmp(string_get_cstr(s1), "Hello") == 0);
string_clean(s1);
string_set_str (s1, "Hello beautiful world!");
assert (string_get_length(s1) == 22);
assert (strcmp(string_get_cstr(s1), "Hello beautiful world!") == 0);
string_clean(s1);
string_set_str(s1, "Hello");
string_cat_str(s1, ", world!");
assert (string_get_length(s1) == 13);
assert (string_cmp_str(s1, "Hello, world!") == 0);
string_set_str(s2, "Hello, world!");
assert (string_cmp(s1, s2) == 0);
string_set_str(s2, "hELLO, world!");
assert (string_cmpi(s1, s2) == 0);
i = string_search_char (s1, ',');
assert (i == 5);
i = string_search_char (s1, 'Z');
assert (i == STRING_FAILURE);
i = string_search_rchar (s1, 'o');
assert (i == 8);
i = string_search_rchar (s1, 'Z');
assert (i == STRING_FAILURE);
i = string_search_str (s1, "world");
assert (i == 7);
i = string_search_str (s1, "World");
assert (i == STRING_FAILURE);
string_set (s2, s1);
j = string_search_char (s1, ',');
string_left (s1, j);
assert (string_cmp_str (s1, "Hello") == 0);
string_set (s1, s2);
j = string_search_char (s1, ',');
string_right (s1, j);
assert (string_cmp_str (s1, ", world!") == 0);
string_set (s1, s2);
j = string_search_char (s1, ',');
string_mid (s1, j, 2);
assert (string_cmp_str (s1, ", ") == 0);
string_set (s1, s2);
b = string_replace_str (s1, "world", "Paul");
assert (b == true);
assert (string_cmp_str (s1, "Hello, Paul!") == 0);
b = string_replace_str (s1, "world", "Paul");
assert (b == false);
assert (string_cmp_str (s1, "Hello, Paul!") == 0);
b = string_replace_str (s1, "Paul", "Juliette");
assert (b == true);
assert (string_cmp_str (s1, "Hello, Juliette!") == 0);
string_printf (s1, "There is %d Paul!", 2);
assert (string_cmp_str (s1, "There is 2 Paul!") == 0);
assert (string_get_length (s1) == 16);
string_set_str(s1, "Hello, world()\n");
i = string_cspn(s1, ",()");
assert (i == 5);
string_set_str(s1, "Hello (world)\n");
i = string_cspn(s1, ",()");
assert (i == 6);
string_set_str(s1, "Hello (world)\n");
i = string_spn(s1, "Hel");
assert (i == 4);
string_clear (s1);
string_init (s1);
assert (string_cmp_str (s1, "") == 0);
assert (string_cmpi (s1, s1) == 0);
string_set_str(s2, "Hello");
string_cat (s2, s1);
assert (string_cmp_str (s2, "Hello") == 0);
string_set_str(s2, "Hello");
string_cat (s1, s2);
assert (string_cmp_str (s1, "Hello") == 0);
string_clear (s1);
string_init (s1);
string_left(s1, 100);
assert (string_empty_p (s1));
assert (string_get_length (s1) == 0);
assert (string_cmp_str (s1, "") == 0);
string_right(s1, 100);
assert (string_empty_p (s1));
assert (string_get_length (s1) == 0);
assert (string_cmp_str (s1, "") == 0);
string_set_str(s2, "Hello");
string_set (s2, s1);
assert (string_empty_p (s2));
assert (string_get_length (s2) == 0);
assert (string_cmp_str (s2, "") == 0);
string_set_str(s1, "Hello, world!");
assert(string_start_with_str_p(s1, "Hello") == true);
assert(string_start_with_str_p(s1, "Help") == false);
string_set_str(s2, "H");
assert(string_start_with_string_p(s1, s2) == true);
string_cat_printf(s1, " %d little %s.", 10, "suns");
string_set_str(s2, "Hello, world! 10 little suns.");
assert(string_equal_p(s1, s2) == true);
string_set_str(s1, " \r\n\t HELLO \n\r\t");
string_strim(s1);
assert (string_cmp_str (s1, "HELLO") == 0);
string_set_str(s1, " \r\n\t \n\r\t");
string_strim(s1);
assert (string_cmp_str (s1, "") == 0);
string_set_str(s1, "Hello world");
string_get_str(s2, s1, false);
assert (string_cmp_str (s2, "\"Hello world\"") == 0);
string_set_str(s1, "Hello \"world\"");
string_get_str(s2, s1, true);
assert (string_cmp_str (s2, "\"Hello world\"\"Hello \\\"world\\\"\"") == 0);
string_set_str(s1, "Hello \"world\"");
FILE *f = fopen ("a.dat", "wt");
assert (f != NULL);
string_out_str (f, s1);
fclose (f);
f = fopen("a.dat", "rt");
assert (f != NULL);
b = string_in_str(s2, f);
assert(b);
fclose(f);
assert (string_equal_p(s1, s2));
string_clear (s1);
}
+115
View File
@@ -0,0 +1,115 @@
/*
* Copyright (c) 2017, 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 <stdio.h>
#include <gmp.h>
#include "m-string.h"
#include "m-tuple.h"
TUPLE_DEF2(pair,
(key, string_t, (INIT(string_init), INIT_SET(string_init_set), SET(string_set), CLEAR(string_clear))),
(value, mpz_t, (INIT(mpz_init), INIT_SET(mpz_init_set), SET(mpz_set), CLEAR(mpz_clear) )))
// No cmp defined
TUPLE_DEF2(triple,
(key, string_t, (INIT(string_init), INIT_SET(string_init_set), SET(string_set), CLEAR(string_clear), CMP(string_cmp))),
(value, mpz_t, (INIT(mpz_init), INIT_SET(mpz_init_set), SET(mpz_set), CLEAR(mpz_clear) )),
(data, const char *, ()))
// cmp shall be defined
TUPLE_DEF2(pair3,
(key, string_t, (INIT(string_init), INIT_SET(string_init_set), SET(string_set), CLEAR(string_clear), CMP(string_cmp), HASH(string_hash), EQUAL(string_equal_p))),
(value, mpz_t, (INIT(mpz_init), INIT_SET(mpz_init_set), SET(mpz_set), CLEAR(mpz_clear), CMP(mpz_cmp) )))
TUPLE_DEF2(pair_str,
(vala, string_t, STRING_OPLIST),
(valb, string_t, STRING_OPLIST))
static void check_io(void)
{
M_LET(s, STRING_OPLIST)
M_LET(pair, TUPLE_OPLIST(pair_str, STRING_OPLIST, STRING_OPLIST))
M_LET(pair2, TUPLE_OPLIST(pair_str, STRING_OPLIST, STRING_OPLIST)) {
string_set_str(pair->vala, "Hello");
string_set_str(pair->valb, "World");
pair_str_get_str(s, pair, false);
assert(string_cmp_str(s, "(\"Hello\",\"World\")") == 0);
FILE *f = fopen("a.dat", "wt");
if (!f) abort();
pair_str_out_str(f, pair);
fclose(f);
f = fopen("a.dat", "rt");
if (!f) abort();
bool b = pair_str_in_str(pair2, f);
assert (b);
fclose(f);
assert (pair_str_equal_p(pair, pair2) == true);
}
}
int main(void)
{
pair_t p1, p2;
pair_init (p1);
string_set_str(p1->key, "HELLO");
mpz_set_str(p1->value, "1742", 10);
pair_init_set (p2, p1);
assert(mpz_cmp_ui(p2->value, 1742) == 0);
assert(string_equal_str_p(p2->key, "HELLO"));
string_set_str(p2->key, "HELLO WORLD");
mpz_set_str(p2->value, "174217", 10);
pair_set(p1, p2);
assert(mpz_cmp_ui(p1->value, 174217) == 0);
assert(string_equal_str_p(p1->key, "HELLO WORLD"));
pair_clear(p1);
pair_clear(p2);
string_t s;
mpz_t z;
pair3_t p3, p4;
string_init_set_str(s, "HELLO");
mpz_init_set_ui (z, 1442);
pair3_init_set2(p3, s, z);
pair3_init_set2(p4, s, z);
int i = pair3_cmp (p3, p4);
assert (i == 0);
mpz_set_ui (z, 1443);
pair3_set_value(p4, z);
i = pair3_cmp (p3, p4);
assert (i < 0);
mpz_set_ui (z, 1442);
string_set_str(s, "HELLN");
pair3_set_key(p4, s);
i = pair3_cmp (p3, p4);
assert (i > 0);
pair3_clear(p3);
pair3_clear(p4);
string_clear(s);
mpz_clear(z);
check_io();
return 0;
}
+62
View File
@@ -0,0 +1,62 @@
/*
* Copyright (c) 2017, 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 "m-list.h"
LIST_DEF(dbl, double)
void f_push_back(list_dbl_t list)
{
list_dbl_push_back(list, 2.0);
}
double f_iterator(list_dbl_t list)
{
list_it_dbl_t it;
double s = 0.0;
for(list_dbl_it(it, list); !list_dbl_end_p(it); list_dbl_next(it))
s += *list_dbl_cref(it);
return s;
}
struct tata {
int x;
};
struct tutu {
void (* const init) (void);
void (* const add) (int *t);
struct tata *data;
};
int f(int p)
{
struct tata el_data;
inline void __attribute__((always_inline)) el_init (void) { el_data.x = 0; };
inline void __attribute__((always_inline)) el_add (int *t) { el_data.x += *t; };
const struct tutu el = { .init = el_init, .add = el_add, .data = &el_data };
el.init();
el.add (&p);
return 0;
}
+37
View File
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2017, 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 "m-array.h"
ARRAY_DEF(unsigned int, uint)
unsigned int add_two(const array_uint_t tab, size_t i, size_t j)
{
return *array_uint_cref(tab, i) + *array_uint_cref(tab, j);
}
void size_to_exactly(array_uint_t tab, size_t n)
{
array_uint_resize(tab, 0);
array_uint_resize(tab, n);
}
+18
View File
@@ -0,0 +1,18 @@
#include <math.h>
#include "m-array.h"
ARRAY_DEF(double, double)
int main(void) {
const int n = 250000000;
const double x = 0.001;
M_LET(v, ARRAY_OPLIST(double)) {
array_double_resize(v, n);
#pragma omp parallel for
for (int i = 0; i < array_double_size(v); ++i) {
array_double_set_at(v, i, cos(i * x));
}
}
return 0;
}