1 /* Test of uN_move() functions.
2 Copyright (C) 2010-2011 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program 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
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2010. */
22 /* Test copying operations with disjoint source and destination. */
24 static const UNIT src[] = { 'c', 'l', 'i', 'm', 'a', 't', 'e' };
27 for (n = 0; n <= SIZEOF (src); n++)
29 UNIT dest[1 + SIZEOF (src) + 1] =
30 { MAGIC, MAGIC, MAGIC, MAGIC, MAGIC, MAGIC, MAGIC, MAGIC, MAGIC };
34 ret = U_MOVE (dest + 1, src, n);
35 ASSERT (ret == dest + 1);
36 ASSERT (dest[0] == MAGIC);
37 for (i = 0; i < n; i++)
38 ASSERT (dest[1 + i] == src[i]);
39 ASSERT (dest[1 + n] == MAGIC);
43 /* Test copying operations with overlap, in-place. */
45 static const UNIT src[] = { 'c', 'l', 'i', 'm', 'a', 't', 'e' };
48 for (n = 0; n <= SIZEOF (src); n++)
50 UNIT dest[1 + SIZEOF (src) + 1];
55 for (i = 0; i < n; i++)
59 ret = U_MOVE (dest + 1, dest + 1, n);
60 ASSERT (ret == dest + 1);
61 ASSERT (dest[0] == MAGIC);
62 for (i = 0; i < n; i++)
63 ASSERT (dest[1 + i] == src[i]);
64 ASSERT (dest[1 + n] == MAGIC);
68 /* Test copying operations with overlap, moving downward. */
70 static const UNIT src[] = { 'c', 'l', 'i', 'm', 'a', 't', 'e' };
71 static const UNIT src2[] = { 'C', 'L', 'I', 'M', 'A', 'T', 'E' };
74 ASSERT (SIZEOF (src) == SIZEOF (src2));
75 for (d = 0; d <= SIZEOF (src); d++)
79 for (n = 0; n <= SIZEOF (src); n++)
81 UNIT dest[1 + 2 * SIZEOF (src) + 1];
86 for (i = 0; i < SIZEOF (src2); i++)
87 dest[1 + i] = src2[i];
88 for (i = 0; i < SIZEOF (src); i++)
89 dest[1 + SIZEOF (src) + i] = src[i];
90 dest[1 + 2 * SIZEOF (src)] = MAGIC;
93 U_MOVE (dest + 1 + SIZEOF (src) - d, dest + 1 + SIZEOF (src), n);
94 ASSERT (ret == dest + 1 + SIZEOF (src) - d);
95 ASSERT (dest[0] == MAGIC);
96 for (i = 0; i < SIZEOF (src) - d; i++)
97 ASSERT (dest[1 + i] == src2[i]);
98 for (i = 0; i < n; i++)
99 ASSERT (dest[1 + SIZEOF (src) - d + i] == src[i]);
100 for (i = SIZEOF (src) - d + n; i < SIZEOF (src2); i++)
101 ASSERT (dest[1 + i] == src2[i]);
102 for (i = (n >= d ? n - d : 0); i < SIZEOF (src); i++)
103 ASSERT (dest[1 + SIZEOF (src) + i] == src[i]);
104 ASSERT (dest[1 + 2 * SIZEOF (src)] == MAGIC);
109 /* Test copying operations with overlap, moving upward. */
111 static const UNIT src[] = { 'c', 'l', 'i', 'm', 'a', 't', 'e' };
112 static const UNIT src2[] = { 'C', 'L', 'I', 'M', 'A', 'T', 'E' };
115 ASSERT (SIZEOF (src) == SIZEOF (src2));
116 for (d = 0; d <= SIZEOF (src); d++)
120 for (n = 0; n <= SIZEOF (src); n++)
122 UNIT dest[1 + 2 * SIZEOF (src) + 1];
127 for (i = 0; i < SIZEOF (src); i++)
128 dest[1 + i] = src[i];
129 for (i = 0; i < SIZEOF (src2); i++)
130 dest[1 + SIZEOF (src) + i] = src2[i];
131 dest[1 + 2 * SIZEOF (src)] = MAGIC;
133 ret = U_MOVE (dest + 1 + d, dest + 1, n);
134 ASSERT (ret == dest + 1 + d);
135 ASSERT (dest[0] == MAGIC);
136 for (i = 0; i < d; i++)
137 ASSERT (dest[1 + i] == src[i]);
138 for (i = 0; i < n; i++)
139 ASSERT (dest[1 + d + i] == src[i]);
140 for (i = d + n; i < SIZEOF (src); i++)
141 ASSERT (dest[1 + i] == src[i]);
142 for (i = (d + n >= SIZEOF (src) ? d + n - SIZEOF (src) : 0);
145 ASSERT (dest[1 + SIZEOF (src) + i] == src2[i]);
146 ASSERT (dest[1 + 2 * SIZEOF (src)] == MAGIC);