annotate libbacktrace/stest.c @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 1830386684a0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* stest.c -- Test for libbacktrace internal sort function
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2 Copyright (C) 2012-2020 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
3 Written by Ian Lance Taylor, Google.
kono
parents:
diff changeset
4
kono
parents:
diff changeset
5 Redistribution and use in source and binary forms, with or without
kono
parents:
diff changeset
6 modification, are permitted provided that the following conditions are
kono
parents:
diff changeset
7 met:
kono
parents:
diff changeset
8
kono
parents:
diff changeset
9 (1) Redistributions of source code must retain the above copyright
kono
parents:
diff changeset
10 notice, this list of conditions and the following disclaimer.
kono
parents:
diff changeset
11
kono
parents:
diff changeset
12 (2) Redistributions in binary form must reproduce the above copyright
kono
parents:
diff changeset
13 notice, this list of conditions and the following disclaimer in
kono
parents:
diff changeset
14 the documentation and/or other materials provided with the
kono
parents:
diff changeset
15 distribution.
kono
parents:
diff changeset
16
kono
parents:
diff changeset
17 (3) The name of the author may not be used to
kono
parents:
diff changeset
18 endorse or promote products derived from this software without
kono
parents:
diff changeset
19 specific prior written permission.
kono
parents:
diff changeset
20
kono
parents:
diff changeset
21 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
kono
parents:
diff changeset
22 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
kono
parents:
diff changeset
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
kono
parents:
diff changeset
24 DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
kono
parents:
diff changeset
25 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
kono
parents:
diff changeset
26 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
kono
parents:
diff changeset
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
kono
parents:
diff changeset
28 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
kono
parents:
diff changeset
29 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
kono
parents:
diff changeset
30 IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
kono
parents:
diff changeset
31 POSSIBILITY OF SUCH DAMAGE. */
kono
parents:
diff changeset
32
kono
parents:
diff changeset
33 #include "config.h"
kono
parents:
diff changeset
34
kono
parents:
diff changeset
35 #include <stdio.h>
kono
parents:
diff changeset
36 #include <stdlib.h>
kono
parents:
diff changeset
37 #include <string.h>
kono
parents:
diff changeset
38 #include <sys/types.h>
kono
parents:
diff changeset
39
kono
parents:
diff changeset
40 #include "backtrace.h"
kono
parents:
diff changeset
41 #include "internal.h"
kono
parents:
diff changeset
42
kono
parents:
diff changeset
43 /* Test the local qsort implementation. */
kono
parents:
diff changeset
44
kono
parents:
diff changeset
45 #define MAX 10
kono
parents:
diff changeset
46
kono
parents:
diff changeset
47 struct test
kono
parents:
diff changeset
48 {
kono
parents:
diff changeset
49 size_t count;
kono
parents:
diff changeset
50 int input[MAX];
kono
parents:
diff changeset
51 int output[MAX];
kono
parents:
diff changeset
52 };
kono
parents:
diff changeset
53
kono
parents:
diff changeset
54 static struct test tests[] =
kono
parents:
diff changeset
55 {
kono
parents:
diff changeset
56 {
kono
parents:
diff changeset
57 10,
kono
parents:
diff changeset
58 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
kono
parents:
diff changeset
59 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
kono
parents:
diff changeset
60 },
kono
parents:
diff changeset
61 {
kono
parents:
diff changeset
62 9,
kono
parents:
diff changeset
63 { 1, 2, 3, 4, 5, 6, 7, 8, 9 },
kono
parents:
diff changeset
64 { 1, 2, 3, 4, 5, 6, 7, 8, 9 }
kono
parents:
diff changeset
65 },
kono
parents:
diff changeset
66 {
kono
parents:
diff changeset
67 10,
kono
parents:
diff changeset
68 { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 },
kono
parents:
diff changeset
69 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
kono
parents:
diff changeset
70 },
kono
parents:
diff changeset
71 {
kono
parents:
diff changeset
72 9,
kono
parents:
diff changeset
73 { 9, 8, 7, 6, 5, 4, 3, 2, 1 },
kono
parents:
diff changeset
74 { 1, 2, 3, 4, 5, 6, 7, 8, 9 },
kono
parents:
diff changeset
75 },
kono
parents:
diff changeset
76 {
kono
parents:
diff changeset
77 10,
kono
parents:
diff changeset
78 { 2, 4, 6, 8, 10, 1, 3, 5, 7, 9 },
kono
parents:
diff changeset
79 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
kono
parents:
diff changeset
80 },
kono
parents:
diff changeset
81 {
kono
parents:
diff changeset
82 5,
kono
parents:
diff changeset
83 { 4, 5, 3, 1, 2 },
kono
parents:
diff changeset
84 { 1, 2, 3, 4, 5 },
kono
parents:
diff changeset
85 },
kono
parents:
diff changeset
86 {
kono
parents:
diff changeset
87 5,
kono
parents:
diff changeset
88 { 1, 1, 1, 1, 1 },
kono
parents:
diff changeset
89 { 1, 1, 1, 1, 1 },
kono
parents:
diff changeset
90 },
kono
parents:
diff changeset
91 {
kono
parents:
diff changeset
92 5,
kono
parents:
diff changeset
93 { 1, 1, 2, 1, 1 },
kono
parents:
diff changeset
94 { 1, 1, 1, 1, 2 },
kono
parents:
diff changeset
95 },
kono
parents:
diff changeset
96 {
kono
parents:
diff changeset
97 5,
kono
parents:
diff changeset
98 { 2, 1, 1, 1, 1 },
kono
parents:
diff changeset
99 { 1, 1, 1, 1, 2 },
kono
parents:
diff changeset
100 },
kono
parents:
diff changeset
101 };
kono
parents:
diff changeset
102
kono
parents:
diff changeset
103 static int
kono
parents:
diff changeset
104 compare (const void *a, const void *b)
kono
parents:
diff changeset
105 {
kono
parents:
diff changeset
106 const int *ai = (const int *) a;
kono
parents:
diff changeset
107 const int *bi = (const int *) b;
kono
parents:
diff changeset
108
kono
parents:
diff changeset
109 return *ai - *bi;
kono
parents:
diff changeset
110 }
kono
parents:
diff changeset
111
kono
parents:
diff changeset
112 int
kono
parents:
diff changeset
113 main (int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
kono
parents:
diff changeset
114 {
kono
parents:
diff changeset
115 int failures;
kono
parents:
diff changeset
116 size_t i;
kono
parents:
diff changeset
117 int a[MAX];
kono
parents:
diff changeset
118
kono
parents:
diff changeset
119 failures = 0;
kono
parents:
diff changeset
120 for (i = 0; i < sizeof tests / sizeof tests[0]; i++)
kono
parents:
diff changeset
121 {
kono
parents:
diff changeset
122 memcpy (a, tests[i].input, tests[i].count * sizeof (int));
kono
parents:
diff changeset
123 backtrace_qsort (a, tests[i].count, sizeof (int), compare);
kono
parents:
diff changeset
124 if (memcmp (a, tests[i].output, tests[i].count * sizeof (int)) != 0)
kono
parents:
diff changeset
125 {
kono
parents:
diff changeset
126 size_t j;
kono
parents:
diff changeset
127
kono
parents:
diff changeset
128 fprintf (stderr, "test %d failed:", (int) i);
kono
parents:
diff changeset
129 for (j = 0; j < tests[i].count; j++)
kono
parents:
diff changeset
130 fprintf (stderr, " %d", a[j]);
kono
parents:
diff changeset
131 fprintf (stderr, "\n");
kono
parents:
diff changeset
132 ++failures;
kono
parents:
diff changeset
133 }
kono
parents:
diff changeset
134 }
kono
parents:
diff changeset
135
kono
parents:
diff changeset
136 exit (failures > 0 ? EXIT_FAILURE : EXIT_SUCCESS);
kono
parents:
diff changeset
137 }