Allow IPv6 address entry in tools>ping - Loosens valid character check
[tomato/davidwu.git] / release / src / router / lzo / tests / align.c
blobbc15663d21532c40a9f06523fce746cc6eb2e5ee
1 /* align.c -- test alignment (important for 16-bit systems)
3 This file is part of the LZO real-time data compression library.
5 Copyright (C) 2011 Markus Franz Xaver Johannes Oberhumer
6 Copyright (C) 2010 Markus Franz Xaver Johannes Oberhumer
7 Copyright (C) 2009 Markus Franz Xaver Johannes Oberhumer
8 Copyright (C) 2008 Markus Franz Xaver Johannes Oberhumer
9 Copyright (C) 2007 Markus Franz Xaver Johannes Oberhumer
10 Copyright (C) 2006 Markus Franz Xaver Johannes Oberhumer
11 Copyright (C) 2005 Markus Franz Xaver Johannes Oberhumer
12 Copyright (C) 2004 Markus Franz Xaver Johannes Oberhumer
13 Copyright (C) 2003 Markus Franz Xaver Johannes Oberhumer
14 Copyright (C) 2002 Markus Franz Xaver Johannes Oberhumer
15 Copyright (C) 2001 Markus Franz Xaver Johannes Oberhumer
16 Copyright (C) 2000 Markus Franz Xaver Johannes Oberhumer
17 Copyright (C) 1999 Markus Franz Xaver Johannes Oberhumer
18 Copyright (C) 1998 Markus Franz Xaver Johannes Oberhumer
19 Copyright (C) 1997 Markus Franz Xaver Johannes Oberhumer
20 Copyright (C) 1996 Markus Franz Xaver Johannes Oberhumer
21 All Rights Reserved.
23 The LZO library is free software; you can redistribute it and/or
24 modify it under the terms of the GNU General Public License as
25 published by the Free Software Foundation; either version 2 of
26 the License, or (at your option) any later version.
28 The LZO library is distributed in the hope that it will be useful,
29 but WITHOUT ANY WARRANTY; without even the implied warranty of
30 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 GNU General Public License for more details.
33 You should have received a copy of the GNU General Public License
34 along with the LZO library; see the file COPYING.
35 If not, write to the Free Software Foundation, Inc.,
36 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
38 Markus F.X.J. Oberhumer
39 <markus@oberhumer.com>
40 http://www.oberhumer.com/opensource/lzo/
44 #if 0
45 #include "src/lzo_conf.h"
46 #include "src/lzo_ptr.h"
47 #endif
48 #include "lzo/lzoconf.h"
50 /* utility layer */
51 #define WANT_LZO_MALLOC 1
52 #include "examples/portab.h"
55 int opt_verbose = 0;
58 /*************************************************************************
60 **************************************************************************/
62 unsigned long align_test(lzo_bytep block, lzo_uint len, lzo_uint step)
64 lzo_bytep b1 = block;
65 lzo_bytep b2 = block;
66 lzo_bytep k1 = NULL;
67 lzo_bytep k2 = NULL;
68 lzo_bytep k;
69 lzo_bytep x;
70 lzo_uint offset = 0;
71 unsigned long i = 0;
73 assert(step > 0);
74 assert(step <= 65536L);
75 assert((step & (step - 1)) == 0);
77 for (offset = step; offset < len; offset += step)
79 k1 = LZO_PTR_ALIGN_UP(b1+1,step);
80 k2 = b2 + offset;
81 if (k1 != k2)
83 printf("error 1: i %lu step %ld offset %ld: "
84 "%p (%ld) %p (%ld)\n",
85 i, (long) step, (long) offset,
86 k1, (long) (k1 - block),
87 k2, (long) (k2 - block));
88 return 0;
90 if (k1 - step != b1)
92 printf("error 2: i %lu step %ld offset %ld: "
93 "%p (%ld) %p (%ld)\n",
94 i, (long) step, (long) offset,
95 b1, (long) (b1 - block),
96 k1, (long) (k1 - block));
97 return 0;
100 assert(k1 > b1);
101 assert(k2 > b2);
102 assert((lzo_uint)(k2 - b2) == offset);
103 assert(k1 - offset == b2);
104 #if defined(PTR_ALIGNED_4)
105 if (step == 4)
107 assert(PTR_ALIGNED_4(k1));
108 assert(PTR_ALIGNED_4(k2));
109 assert(PTR_ALIGNED2_4(k1,k2));
111 #endif
112 #if defined(PTR_ALIGNED_8)
113 if (step == 8)
115 assert(PTR_ALIGNED_8(k1));
116 assert(PTR_ALIGNED_8(k2));
117 assert(PTR_ALIGNED2_8(k1,k2));
119 #endif
120 #if defined(PTR_LINEAR)
121 assert((PTR_LINEAR(k1) & (step-1)) == 0);
122 assert((PTR_LINEAR(k2) & (step-1)) == 0);
123 #endif
125 for (k = b1 + 1; k <= k1; k++)
127 x = LZO_PTR_ALIGN_UP(k,step);
128 if (x != k1)
130 printf("error 3: base: %p %p %p i %lu step %ld offset %ld: "
131 "%p (%ld) %p (%ld) %p (%ld)\n",
132 block, b1, b2,
133 i, (long) step, (long) offset,
134 k1, (long) (k1 - block),
135 k, (long) (k - block),
136 x, (long) (x - block));
137 return 0;
141 b1 = k1;
142 i++;
145 return i;
149 /*************************************************************************
151 **************************************************************************/
153 #define BLOCK_LEN (128*1024ul)
155 int main(int argc, char *argv[])
157 lzo_bytep buf;
158 lzo_uint step;
160 if (argc >= 2 && strcmp(argv[1],"-v") == 0)
161 opt_verbose = 1;
163 if (lzo_init() != LZO_E_OK)
165 printf("lzo_init() failed !!!\n");
166 return 3;
168 buf = (lzo_bytep) lzo_malloc(2*BLOCK_LEN + 256);
169 if (buf == NULL)
171 printf("out of memory\n");
172 return 2;
175 #if defined(lzo_uintptr_t)
176 printf("Align init: %p ( 0x%lx )\n", buf, (unsigned long) (lzo_uintptr_t) buf);
177 #elif defined(__LZO_MMODEL_HUGE)
178 printf("Align init: %p ( 0x%lx )\n", buf, (unsigned long) buf);
179 #else
180 printf("Align init: %p ( 0x%lx )\n", buf, (unsigned long) (size_t) buf);
181 #endif
183 for (step = 1; step <= 65536L; step *= 2)
185 lzo_bytep block = buf;
186 unsigned long n;
187 unsigned gap;
189 gap = __lzo_align_gap(block,step);
190 block = LZO_PTR_ALIGN_UP(block,step);
191 if (opt_verbose >= 1)
192 printf("STEP %5lu: GAP: %5lu %p %p %5lu\n",
193 (unsigned long) step, (unsigned long) gap, buf, block,
194 (unsigned long) (block - buf));
195 n = align_test(block,BLOCK_LEN,step);
196 if (n == 0)
197 return 1;
198 if ((n + 1) * step != BLOCK_LEN)
200 printf("error 4: %ld %lu\n", (long)step, n);
201 return 1;
205 lzo_free(buf);
206 printf("Alignment test passed.\n");
207 return 0;
212 vi:ts=4:et