summaryrefslogtreecommitdiff
path: root/bitmask_android/lzo/tests/align.c
blob: bc15663d21532c40a9f06523fce746cc6eb2e5ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/* align.c -- test alignment (important for 16-bit systems)

   This file is part of the LZO real-time data compression library.

   Copyright (C) 2011 Markus Franz Xaver Johannes Oberhumer
   Copyright (C) 2010 Markus Franz Xaver Johannes Oberhumer
   Copyright (C) 2009 Markus Franz Xaver Johannes Oberhumer
   Copyright (C) 2008 Markus Franz Xaver Johannes Oberhumer
   Copyright (C) 2007 Markus Franz Xaver Johannes Oberhumer
   Copyright (C) 2006 Markus Franz Xaver Johannes Oberhumer
   Copyright (C) 2005 Markus Franz Xaver Johannes Oberhumer
   Copyright (C) 2004 Markus Franz Xaver Johannes Oberhumer
   Copyright (C) 2003 Markus Franz Xaver Johannes Oberhumer
   Copyright (C) 2002 Markus Franz Xaver Johannes Oberhumer
   Copyright (C) 2001 Markus Franz Xaver Johannes Oberhumer
   Copyright (C) 2000 Markus Franz Xaver Johannes Oberhumer
   Copyright (C) 1999 Markus Franz Xaver Johannes Oberhumer
   Copyright (C) 1998 Markus Franz Xaver Johannes Oberhumer
   Copyright (C) 1997 Markus Franz Xaver Johannes Oberhumer
   Copyright (C) 1996 Markus Franz Xaver Johannes Oberhumer
   All Rights Reserved.

   The LZO library is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of
   the License, or (at your option) any later version.

   The LZO library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with the LZO library; see the file COPYING.
   If not, write to the Free Software Foundation, Inc.,
   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

   Markus F.X.J. Oberhumer
   <markus@oberhumer.com>
   http://www.oberhumer.com/opensource/lzo/
 */


#if 0
#include "src/lzo_conf.h"
#include "src/lzo_ptr.h"
#endif
#include "lzo/lzoconf.h"

/* utility layer */
#define WANT_LZO_MALLOC 1
#include "examples/portab.h"


int opt_verbose = 0;


/*************************************************************************
//
**************************************************************************/

unsigned long align_test(lzo_bytep block, lzo_uint len, lzo_uint step)
{
    lzo_bytep b1 = block;
    lzo_bytep b2 = block;
    lzo_bytep k1 = NULL;
    lzo_bytep k2 = NULL;
    lzo_bytep k;
    lzo_bytep x;
    lzo_uint offset = 0;
    unsigned long i = 0;

    assert(step > 0);
    assert(step <= 65536L);
    assert((step & (step - 1)) == 0);

    for (offset = step; offset < len; offset += step)
    {
        k1 = LZO_PTR_ALIGN_UP(b1+1,step);
        k2 = b2 + offset;
        if (k1 != k2)
        {
            printf("error 1: i %lu step %ld offset %ld: "
                   "%p (%ld) %p (%ld)\n",
                   i, (long) step, (long) offset,
                   k1, (long) (k1 - block),
                   k2, (long) (k2 - block));
            return 0;
        }
        if (k1 - step != b1)
        {
            printf("error 2: i %lu step %ld offset %ld: "
                   "%p (%ld) %p (%ld)\n",
                   i, (long) step, (long) offset,
                   b1, (long) (b1 - block),
                   k1, (long) (k1 - block));
            return 0;
        }

        assert(k1 > b1);
        assert(k2 > b2);
        assert((lzo_uint)(k2 - b2) == offset);
        assert(k1 - offset == b2);
#if defined(PTR_ALIGNED_4)
        if (step == 4)
        {
            assert(PTR_ALIGNED_4(k1));
            assert(PTR_ALIGNED_4(k2));
            assert(PTR_ALIGNED2_4(k1,k2));
        }
#endif
#if defined(PTR_ALIGNED_8)
        if (step == 8)
        {
            assert(PTR_ALIGNED_8(k1));
            assert(PTR_ALIGNED_8(k2));
            assert(PTR_ALIGNED2_8(k1,k2));
        }
#endif
#if defined(PTR_LINEAR)
        assert((PTR_LINEAR(k1) & (step-1)) == 0);
        assert((PTR_LINEAR(k2) & (step-1)) == 0);
#endif

        for (k = b1 + 1; k <= k1; k++)
        {
            x = LZO_PTR_ALIGN_UP(k,step);
            if (x != k1)
            {
                printf("error 3: base: %p %p %p  i %lu step %ld offset %ld: "
                       "%p (%ld) %p (%ld) %p (%ld)\n",
                       block, b1, b2,
                       i, (long) step, (long) offset,
                       k1, (long) (k1 - block),
                       k, (long) (k - block),
                       x, (long) (x - block));
                return 0;
            }
        }

        b1 = k1;
        i++;
    }

    return i;
}


/*************************************************************************
//
**************************************************************************/

#define BLOCK_LEN   (128*1024ul)

int main(int argc, char *argv[])
{
    lzo_bytep buf;
    lzo_uint step;

    if (argc >= 2 && strcmp(argv[1],"-v") == 0)
        opt_verbose = 1;

    if (lzo_init() != LZO_E_OK)
    {
        printf("lzo_init() failed !!!\n");
        return 3;
    }
    buf = (lzo_bytep) lzo_malloc(2*BLOCK_LEN + 256);
    if (buf == NULL)
    {
        printf("out of memory\n");
        return 2;
    }

#if defined(lzo_uintptr_t)
    printf("Align init: %p ( 0x%lx )\n", buf, (unsigned long) (lzo_uintptr_t) buf);
#elif defined(__LZO_MMODEL_HUGE)
    printf("Align init: %p ( 0x%lx )\n", buf, (unsigned long) buf);
#else
    printf("Align init: %p ( 0x%lx )\n", buf, (unsigned long) (size_t) buf);
#endif

    for (step = 1; step <= 65536L; step *= 2)
    {
        lzo_bytep block = buf;
        unsigned long n;
        unsigned gap;

        gap = __lzo_align_gap(block,step);
        block = LZO_PTR_ALIGN_UP(block,step);
        if (opt_verbose >= 1)
            printf("STEP %5lu: GAP: %5lu  %p %p %5lu\n",
                   (unsigned long) step, (unsigned long) gap, buf, block,
                   (unsigned long) (block - buf));
        n = align_test(block,BLOCK_LEN,step);
        if (n == 0)
            return 1;
        if ((n + 1) * step != BLOCK_LEN)
        {
            printf("error 4: %ld %lu\n", (long)step, n);
            return 1;
        }
    }

    lzo_free(buf);
    printf("Alignment test passed.\n");
    return 0;
}


/*
vi:ts=4:et
*/