lzo: update to 2.09
[tomato.git] / release / src / router / lzo / tests / align.c
blob59bf8333013c72e0878127b610a7fcba1a4c6ebd
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) 1996-2015 Markus Franz Xaver Johannes Oberhumer
6 All Rights Reserved.
8 The LZO library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of
11 the License, or (at your option) any later version.
13 The LZO library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with the LZO library; see the file COPYING.
20 If not, write to the Free Software Foundation, Inc.,
21 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 Markus F.X.J. Oberhumer
24 <markus@oberhumer.com>
25 http://www.oberhumer.com/opensource/lzo/
29 #if 0
30 #include "src/lzo_conf.h"
31 #include "src/lzo_ptr.h"
32 #endif
33 #include <lzo/lzoconf.h>
35 /* utility layer */
36 #define WANT_LZO_MALLOC 1
37 #include "examples/portab.h"
40 static int opt_verbose = 0;
43 /*************************************************************************
45 **************************************************************************/
47 static unsigned long align_test(lzo_bytep block, lzo_uint len, lzo_uint step)
49 lzo_bytep b1 = block;
50 lzo_bytep b2 = block;
51 lzo_bytep k1 = NULL;
52 lzo_bytep k2 = NULL;
53 lzo_bytep k;
54 lzo_bytep x;
55 lzo_uint offset = 0;
56 unsigned long i = 0;
58 assert(step > 0);
59 assert(step <= 65536ul);
60 assert((step & (step - 1)) == 0);
62 for (offset = step; offset < len; offset += step)
64 k1 = LZO_PTR_ALIGN_UP(b1 + 1, step);
65 k2 = b2 + offset;
66 if (k1 != k2)
68 printf("error 1: i %lu step %ld offset %ld: "
69 "%p (%ld) %p (%ld)\n",
70 i, (long) step, (long) offset,
71 k1, (long) (k1 - block),
72 k2, (long) (k2 - block));
73 return 0;
75 if (k1 - step != b1)
77 printf("error 2: i %lu step %ld offset %ld: "
78 "%p (%ld) %p (%ld)\n",
79 i, (long) step, (long) offset,
80 b1, (long) (b1 - block),
81 k1, (long) (k1 - block));
82 return 0;
85 assert(k1 > b1);
86 assert(k2 > b2);
87 assert((lzo_uint)(k2 - b2) == offset);
88 assert(k1 - offset == b2);
89 #if defined(PTR_ALIGNED_4)
90 if (step == 4)
92 assert(PTR_ALIGNED_4(k1));
93 assert(PTR_ALIGNED_4(k2));
94 assert(PTR_ALIGNED2_4(k1,k2));
96 #endif
97 #if defined(PTR_ALIGNED_8)
98 if (step == 8)
100 assert(PTR_ALIGNED_8(k1));
101 assert(PTR_ALIGNED_8(k2));
102 assert(PTR_ALIGNED2_8(k1,k2));
104 #endif
105 #if defined(PTR_LINEAR)
106 assert((PTR_LINEAR(k1) & (step-1)) == 0);
107 assert((PTR_LINEAR(k2) & (step-1)) == 0);
108 #endif
110 for (k = b1 + 1; k <= k1; k++)
112 x = LZO_PTR_ALIGN_UP(k, step);
113 if (x != k1)
115 printf("error 3: base: %p %p %p i %lu step %ld offset %ld: "
116 "%p (%ld) %p (%ld) %p (%ld)\n",
117 block, b1, b2,
118 i, (long) step, (long) offset,
119 k1, (long) (k1 - block),
120 k, (long) (k - block),
121 x, (long) (x - block));
122 return 0;
126 b1 = k1;
127 i++;
130 return i;
134 /*************************************************************************
136 **************************************************************************/
138 #define BLOCK_SIZE (128*1024ul)
140 int main(int argc, char *argv[])
142 lzo_bytep buf;
143 lzo_uint step;
145 if (argc >= 2 && strcmp(argv[1],"-v") == 0)
146 opt_verbose = 1;
148 if (lzo_init() != LZO_E_OK)
150 printf("lzo_init() failed !!!\n");
151 return 3;
153 buf = (lzo_bytep) lzo_malloc(2*BLOCK_SIZE + 256);
154 if (buf == NULL)
156 printf("out of memory\n");
157 return 2;
160 #if defined(lzo_uintptr_t)
161 printf("Align init: %p ( 0x%lx )\n", buf, (unsigned long) (lzo_uintptr_t) buf);
162 #elif defined(__LZO_MMODEL_HUGE)
163 printf("Align init: %p ( 0x%lx )\n", buf, (unsigned long) buf);
164 #else
165 printf("Align init: %p ( 0x%lx )\n", buf, (unsigned long) (size_t) buf);
166 #endif
168 for (step = 1; step <= 65536ul; step *= 2)
170 lzo_bytep block = buf;
171 unsigned long n;
172 unsigned gap;
174 gap = __lzo_align_gap(block, step);
175 block = LZO_PTR_ALIGN_UP(block, step);
176 if (opt_verbose >= 1)
177 printf("STEP %5lu: GAP: %5lu %p %p %5lu\n",
178 (unsigned long) step, (unsigned long) gap, buf, block,
179 (unsigned long) (block - buf));
180 n = align_test(block, BLOCK_SIZE, step);
181 if (n == 0)
182 return 1;
183 if ((n + 1) * step != BLOCK_SIZE)
185 printf("error 4: %ld %lu\n", (long)step, n);
186 return 1;
190 lzo_free(buf);
191 printf("Alignment test passed.\n");
192 return 0;
196 /* vim:set ts=4 sw=4 et: */