Commit | Line | Data |
---|---|---|
f9db0c4c JB |
1 | /* Self tests of the copy_bitwise routine for GDB, the GNU debugger. |
2 | ||
42a4f53d | 3 | Copyright (C) 2018-2019 Free Software Foundation, Inc. |
f9db0c4c JB |
4 | |
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 3 of the License, or | |
10 | (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, see <http://www.gnu.org/licenses/>. */ | |
19 | ||
20 | #include "defs.h" | |
268a13a5 | 21 | #include "gdbsupport/selftest.h" |
f9db0c4c JB |
22 | #include "utils.h" |
23 | ||
24 | namespace selftests { | |
25 | ||
26 | /* Helper function for the unit test of copy_bitwise. Convert NBITS bits | |
27 | out of BITS, starting at OFFS, to the respective '0'/'1'-string. MSB0 | |
28 | specifies whether to assume big endian bit numbering. Store the | |
29 | resulting (not null-terminated) string at STR. */ | |
30 | ||
31 | static void | |
32 | bits_to_str (char *str, const gdb_byte *bits, ULONGEST offs, | |
33 | ULONGEST nbits, int msb0) | |
34 | { | |
35 | unsigned int j; | |
36 | size_t i; | |
37 | ||
38 | for (i = offs / 8, j = offs % 8; nbits; i++, j = 0) | |
39 | { | |
40 | unsigned int ch = bits[i]; | |
41 | for (; j < 8 && nbits; j++, nbits--) | |
42 | *str++ = (ch & (msb0 ? (1 << (7 - j)) : (1 << j))) ? '1' : '0'; | |
43 | } | |
44 | } | |
45 | ||
46 | /* Check one invocation of copy_bitwise with the given parameters. */ | |
47 | ||
48 | static void | |
49 | check_copy_bitwise (const gdb_byte *dest, unsigned int dest_offset, | |
50 | const gdb_byte *source, unsigned int source_offset, | |
51 | unsigned int nbits, int msb0) | |
52 | { | |
53 | size_t len = align_up (dest_offset + nbits, 8); | |
54 | char *expected = (char *) alloca (len + 1); | |
55 | char *actual = (char *) alloca (len + 1); | |
56 | gdb_byte *buf = (gdb_byte *) alloca (len / 8); | |
57 | ||
58 | /* Compose a '0'/'1'-string that represents the expected result of | |
59 | copy_bitwise below: | |
60 | Bits from [0, DEST_OFFSET) are filled from DEST. | |
61 | Bits from [DEST_OFFSET, DEST_OFFSET + NBITS) are filled from SOURCE. | |
62 | Bits from [DEST_OFFSET + NBITS, LEN) are filled from DEST. | |
63 | ||
64 | E.g., with: | |
65 | dest_offset: 4 | |
66 | nbits: 2 | |
67 | len: 8 | |
68 | dest: 00000000 | |
69 | source: 11111111 | |
70 | ||
71 | We should end up with: | |
72 | buf: 00001100 | |
73 | DDDDSSDD (D=dest, S=source) | |
74 | */ | |
75 | bits_to_str (expected, dest, 0, len, msb0); | |
76 | bits_to_str (expected + dest_offset, source, source_offset, nbits, msb0); | |
77 | ||
78 | /* Fill BUF with data from DEST, apply copy_bitwise, and convert the | |
79 | result to a '0'/'1'-string. */ | |
80 | memcpy (buf, dest, len / 8); | |
81 | copy_bitwise (buf, dest_offset, source, source_offset, nbits, msb0); | |
82 | bits_to_str (actual, buf, 0, len, msb0); | |
83 | ||
84 | /* Compare the resulting strings. */ | |
85 | expected[len] = actual[len] = '\0'; | |
86 | if (strcmp (expected, actual) != 0) | |
87 | error (_("copy_bitwise %s != %s (%u+%u -> %u)"), | |
88 | expected, actual, source_offset, nbits, dest_offset); | |
89 | } | |
90 | ||
91 | /* Unit test for copy_bitwise. */ | |
92 | ||
93 | static void | |
94 | copy_bitwise_tests (void) | |
95 | { | |
96 | /* Data to be used as both source and destination buffers. The two | |
97 | arrays below represent the lsb0- and msb0- encoded versions of the | |
98 | following bit string, respectively: | |
99 | 00000000 00011111 11111111 01001000 10100101 11110010 | |
100 | This pattern is chosen such that it contains: | |
101 | - constant 0- and 1- chunks of more than a full byte; | |
102 | - 0/1- and 1/0 transitions on all bit positions within a byte; | |
103 | - several sufficiently asymmetric bytes. | |
104 | */ | |
105 | static const gdb_byte data_lsb0[] = { | |
106 | 0x00, 0xf8, 0xff, 0x12, 0xa5, 0x4f | |
107 | }; | |
108 | static const gdb_byte data_msb0[] = { | |
109 | 0x00, 0x1f, 0xff, 0x48, 0xa5, 0xf2 | |
110 | }; | |
111 | ||
112 | constexpr size_t data_nbits = 8 * sizeof (data_lsb0); | |
113 | constexpr unsigned max_nbits = 24; | |
114 | ||
115 | /* Try all combinations of: | |
116 | lsb0/msb0 bit order (using the respective data array) | |
117 | X [0, MAX_NBITS] copy bit width | |
118 | X feasible source offsets for the given copy bit width | |
119 | X feasible destination offsets | |
120 | */ | |
121 | for (int msb0 = 0; msb0 < 2; msb0++) | |
122 | { | |
123 | const gdb_byte *data = msb0 ? data_msb0 : data_lsb0; | |
124 | ||
125 | for (unsigned int nbits = 1; nbits <= max_nbits; nbits++) | |
126 | { | |
127 | const unsigned int max_offset = data_nbits - nbits; | |
128 | ||
129 | for (unsigned source_offset = 0; | |
130 | source_offset <= max_offset; | |
131 | source_offset++) | |
132 | { | |
133 | for (unsigned dest_offset = 0; | |
134 | dest_offset <= max_offset; | |
135 | dest_offset++) | |
136 | { | |
137 | check_copy_bitwise (data + dest_offset / 8, | |
138 | dest_offset % 8, | |
139 | data + source_offset / 8, | |
140 | source_offset % 8, | |
141 | nbits, msb0); | |
142 | } | |
143 | } | |
144 | } | |
145 | ||
146 | /* Special cases: copy all, copy nothing. */ | |
147 | check_copy_bitwise (data_lsb0, 0, data_msb0, 0, data_nbits, msb0); | |
148 | check_copy_bitwise (data_msb0, 0, data_lsb0, 0, data_nbits, msb0); | |
149 | check_copy_bitwise (data, data_nbits - 7, data, 9, 0, msb0); | |
150 | } | |
151 | } | |
152 | ||
153 | } /* namespace selftests */ | |
154 | ||
155 | void | |
156 | _initialize_copy_bitwise_utils_selftests () | |
157 | { | |
158 | selftests::register_test ("copy_bitwise", selftests::copy_bitwise_tests); | |
159 | } |