Lumiera  0.pre.03
»edit your freedom«
test-safeclib.c
Go to the documentation of this file.
1 /*
2  TEST-SAFECLIB - some checked C operations
3 
4  Copyright (C) Lumiera.org
5  2008, Christian Thaeter <ct@pipapo.org>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 * *****************************************************/
22 
29 #include "lib/safeclib.h"
30 #include "lib/tmpbuf.h" /* not factored out yet */
31 #include "lib/test/test.h"
32 
33 #include <sys/time.h>
34 #include <sys/resource.h>
35 
36 TESTS_BEGIN
37 
38 TEST (allocation0)
39 {
40  lumiera_malloc (0);
41  NOTREACHED();
42 }
43 
44 TEST (allocation1024)
45 {
46  void* data[1024];
47  for (int i = 0; i < 1024; ++i)
48  {
49  data[i] = lumiera_malloc (1024);
50  CHECK (data[i]);
51  }
52  for (int i = 0; i < 1024; ++i)
53  {
54  free (data[i]);
55  }
56 }
57 
58 TEST (allocationtoobig)
59 {
60  struct rlimit rl;
61  rl.rlim_cur = 100*1024*1024;
62  rl.rlim_max = 100*1024*1024;
63  setrlimit (RLIMIT_AS, &rl);
64  lumiera_malloc (200*1024*1024);
65  NOTREACHED();
66 }
67 
68 TEST (streq)
69 {
70  CHECK (lumiera_streq ("foo", "foo"));
71  CHECK (lumiera_streq (NULL, NULL));
72  CHECK (!lumiera_streq (NULL, "foo"));
73  CHECK (!lumiera_streq ("foo", NULL));
74  CHECK (!lumiera_streq ("foo", "bar"));
75 }
76 
77 TEST (tmpbuf)
78 {
79  for (int i = 0; i < 256; ++i)
80  {
81  char* buf = lumiera_tmpbuf_provide (1024);
82 
83  for (int j = 0; j < 1024; ++j)
84  {
85  buf[j] = i;
86  }
87  }
88 }
89 
90 
91 TEST (tr0)
92 {
93  char* r = lumiera_tmpbuf_tr (argv[2], "abcdeABCDE0123456789", "ABCDEABCDE0123456789", NULL);
94  printf("%s\n", r?r:"failed");
95 }
96 
97 
98 TEST (tr)
99 {
100  char* r = lumiera_tmpbuf_tr (argv[2], "abcdeABCDE0123456789", "ABCDEABCDE0123456789", "");
101  printf("%s\n", r?r:"failed");
102 }
103 
104 
105 TEST (tr_)
106 {
107  printf("%s\n", lumiera_tmpbuf_tr (argv[2], "abcdeABCDE0123456789", "ABCDEABCDE0123456789", "_"));
108 }
109 
110 
111 TESTS_END
Portable and safe wrappers around some C-Lib functions.
void * lumiera_tmpbuf_provide(size_t size)
Query a thread local tmpbuf.
Definition: tmpbuf.c:89
Helpers and support macros for defining test executables in C.
Round robin temporary buffers.
int lumiera_streq(const char *a, const char *b)
check 2 strings for identity.
Definition: safeclib.c:200
void * lumiera_malloc(size_t size)
Allocate memory.
Definition: safeclib.c:122
char * lumiera_tmpbuf_tr(const char *in, const char *from, const char *to, const char *def)
Translates characters in a string, similar to the shell &#39;tr&#39; utility.
Definition: tmpbuf.c:156