doc: Add screenshot to spiv page.
[gfxprim/pasky.git] / tests / framework / test.c
blob3fbbc8d7f08554cd6724916bbd7405d26c74c8f7
1 /*****************************************************************************
2 * This file is part of gfxprim library. *
3 * *
4 * Gfxprim is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU Lesser General Public *
6 * License as published by the Free Software Foundation; either *
7 * version 2.1 of the License, or (at your option) any later version. *
8 * *
9 * Gfxprim is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12 * Lesser General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with gfxprim; if not, write to the Free Software *
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301 USA *
18 * *
19 * Copyright (C) 2009-2012 Cyril Hrubis <metan@ucw.cz> *
20 * *
21 *****************************************************************************/
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <stdio.h>
26 #include <errno.h>
27 #include <string.h>
29 #include "tst_test.h"
30 #include "tst_malloc_canaries.h"
31 #include "tst_preload_FILE.h"
33 int success_fn(void)
35 tst_msg("This test does nothing");
36 tst_msg("But successfully");
38 return TST_SUCCESS;
41 int sigsegv_fn(void)
43 return *(int*)NULL;
46 int failed_fn(void)
48 return TST_FAILED;
51 int stack_overflow_fn(void)
53 int ret = stack_overflow_fn() + stack_overflow_fn();
55 return ret;
58 int timeout_fn(void)
60 tst_msg("Sleeping for ten seconds");
61 sleep(10);
62 return TST_SUCCESS;
65 int temp_dir_fn(void)
67 char buf[256], *res;
69 /* log current working directory */
70 res = getcwd(buf, sizeof(buf));
71 tst_msg("CWD is '%s'", res);
73 return TST_SUCCESS;
76 int malloc_leak_fn(void)
78 void *p, *q, *r;
80 q = malloc(100);
81 p = malloc(4);
82 p = malloc(3);
83 r = malloc(20);
85 free(p);
86 free(q);
87 free(r);
89 tst_msg("Leaking 1 chunks 4 bytes total");
91 return TST_SUCCESS;
94 int malloc_ok_fn(void)
96 unsigned int perm[20] = {
97 1, 3, 2, 6, 4, 5, 0, 9, 8, 14,
98 7, 11, 13, 10, 12, 19, 17, 16, 15, 18,
101 unsigned int i;
102 void *p[20];
104 for (i = 0; i < 20; i++)
105 p[i] = malloc((7 * i) % 127);
107 for (i = 0; i < 20; i++)
108 free(p[perm[i]]);
110 return TST_SUCCESS;
113 int double_free(void)
115 void *p = malloc(100);
117 free(p);
118 free(p);
120 return TST_SUCCESS;
123 int canary_allocation(void)
125 char *buf = tst_malloc_canary_right(31);
127 int i;
129 for (i = 0; i < 31; i++)
130 buf[i] = 0;
132 tst_msg("About to use address after the buffer with barrier");
134 buf[31] = 0;
136 tst_msg("This is not printed at all");
138 return TST_SUCCESS;
141 int fail_FILE(void)
143 struct tst_fail_FILE failures[] = {
144 {.path = "test_fail_fopen", .call = TST_FAIL_FOPEN, .err = EPERM},
145 {.path = "test_fail_fclose", .call = TST_FAIL_FCLOSE, .err = ENOSPC},
146 {.path = NULL}
149 tst_fail_FILE_register(failures);
151 int fail = 0;
152 FILE *f;
154 f = fopen("test_fail_fclose", "w");
156 if (f == NULL) {
157 tst_msg("Failed to open 'test_fail_fclose' for writing: %s",
158 strerror(errno));
159 fail = 1;
162 tst_msg("Correctly opened 'test_fail_fclose'");
164 int ret = fclose(f);
166 if (ret == 0 || errno != ENOSPC) {
167 tst_msg("Failed to fail to close 'test_fail_fclose'");
168 fail = 1;
171 tst_msg("Correctly failed to close 'test_fail_fclose'");
173 f = fopen("test_fail_fopen", "w");
175 if (f != NULL && errno != EPERM) {
176 tst_msg("Failed to fail to open 'test_fail_fopen'");
177 fclose(f);
178 fail = 1;
181 tst_msg("Correctly failed to open 'test_fail_fopen'");
183 if (fail)
184 return TST_FAILED;
186 return TST_SUCCESS;
189 static int messages_test_fn(void)
191 /* stdout and stderr capture test */
192 printf("This is stdout\n");
193 fprintf(stderr, "This is stderr\n");
195 tst_msg("This is message");
196 tst_warn("This is a warning");
197 tst_err("This is an error");
199 return TST_SUCCESS;
203 * This status is returned when the could not be started
204 * because of unsufficient configuration
206 static int skipped_fn(void)
208 return TST_SKIPPED;
212 * This status is returned when there was failure prior
213 * the actuall testing so we could not test the feature
214 * at all.
216 static int untested_fn(void)
218 return TST_UNTESTED;
221 static int res_fn(void)
223 if (access("test.c", R_OK) == 0)
224 tst_msg("File correctly copied");
226 return TST_SUCCESS;
229 static int fpe_fn(void)
231 /* its volatile so compiler doesn't detect the division by zero */
232 volatile int i = 0;
234 return 1/i;
238 * Let's benchmark memset.
240 static int benchmark_fn(void)
242 char buf[256];
243 unsigned int i;
245 for (i = 0; i < 4000000; i++)
246 memset(buf, i%100, sizeof(buf));
248 return TST_SUCCESS;
251 const struct tst_suite tst_suite = {
252 .suite_name = "Testing Framework Example",
253 .tests = {
254 {.name = "Success test", .tst_fn = success_fn},
255 {.name = "Skipped test", .tst_fn = skipped_fn},
256 {.name = "Untested test", .tst_fn = untested_fn},
257 {.name = "Sigsegv test", .tst_fn = sigsegv_fn},
258 {.name = "Failed test", .tst_fn = failed_fn},
259 {.name = "Stack overflow test", .tst_fn = stack_overflow_fn},
260 {.name = "Timeout test", .tst_fn = timeout_fn, .timeout = 1},
261 {.name = "Tempdir test", .tst_fn = temp_dir_fn, .flags = TST_TMPDIR},
262 {.name = "Mem Leak test", .tst_fn = malloc_leak_fn, .flags = TST_CHECK_MALLOC},
263 {.name = "Mem Ok test", .tst_fn = malloc_ok_fn, .flags = TST_CHECK_MALLOC},
264 {.name = "Double free()", .tst_fn = double_free},
265 {.name = "Canary allocation", .tst_fn = canary_allocation},
266 {.name = "Failed FILE", .tst_fn = fail_FILE, .flags = TST_TMPDIR},
267 {.name = "Resource", .tst_fn = res_fn, .flags = TST_TMPDIR,
268 .res_path = "test.c"},
269 {.name = "FP exception", .tst_fn = fpe_fn},
270 {.name = "Messages test", .tst_fn = messages_test_fn},
271 {.name = "Benchmark test", .tst_fn = benchmark_fn, .bench_iter = 10},
272 {.name = NULL},