Optimize reading/write into sparse array
[openjpeg.git] / src / lib / openjp2 / test_sparse_array.c
1 /*
2  * The copyright in this software is being made available under the 2-clauses
3  * BSD License, included below. This software may be subject to other third
4  * party and contributor rights, including patent rights, and no such rights
5  * are granted under this license.
6  *
7  * Copyright (c) 2017, IntoPix SA <contact@intopix.com>
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #undef NDEBUG
33
34 #include "opj_includes.h"
35
36 int main()
37 {
38     OPJ_UINT32 i, j, w, h;
39     OPJ_INT32 buffer[ 99 * 101 ];
40     OPJ_BOOL ret;
41     opj_sparse_array_int32_t* sa;
42
43     sa = opj_sparse_array_int32_create(0, 1, 1, 1);
44     assert(sa == NULL);
45     opj_sparse_array_int32_free(sa);
46
47     sa = opj_sparse_array_int32_create(1, 0, 1, 1);
48     assert(sa == NULL);
49
50     sa = opj_sparse_array_int32_create(1, 1, 0, 1);
51     assert(sa == NULL);
52
53     sa = opj_sparse_array_int32_create(1, 1, 1, 0);
54     assert(sa == NULL);
55
56     sa = opj_sparse_array_int32_create(99, 101, ~0U, ~0U);
57     assert(sa == NULL);
58
59     sa = opj_sparse_array_int32_create(99, 101, 15, 17);
60     opj_sparse_array_int32_free(sa);
61
62     sa = opj_sparse_array_int32_create(99, 101, 15, 17);
63     ret = opj_sparse_array_int32_read(sa, 0, 0, 0, 1, buffer, 1, 1, OPJ_FALSE);
64     assert(!ret);
65     ret = opj_sparse_array_int32_read(sa, 0, 0, 1, 0, buffer, 1, 1, OPJ_FALSE);
66     assert(!ret);
67     ret = opj_sparse_array_int32_read(sa, 0, 0, 100, 1, buffer, 1, 1, OPJ_FALSE);
68     assert(!ret);
69     ret = opj_sparse_array_int32_read(sa, 0, 0, 1, 102, buffer, 1, 1, OPJ_FALSE);
70     assert(!ret);
71     ret = opj_sparse_array_int32_read(sa, 1, 0, 0, 1, buffer, 1, 1, OPJ_FALSE);
72     assert(!ret);
73     ret = opj_sparse_array_int32_read(sa, 0, 1, 1, 0, buffer, 1, 1, OPJ_FALSE);
74     assert(!ret);
75     ret = opj_sparse_array_int32_read(sa, 99, 101, 99, 101, buffer, 1, 1,
76                                       OPJ_FALSE);
77     assert(!ret);
78
79     buffer[0] = 1;
80     ret = opj_sparse_array_int32_read(sa, 0, 0, 1, 1, buffer, 1, 1, OPJ_FALSE);
81     assert(ret);
82     assert(buffer[0] == 0);
83
84     memset(buffer, 0xFF, sizeof(buffer));
85     ret = opj_sparse_array_int32_read(sa, 0, 0, 99, 101, buffer, 1, 99, OPJ_FALSE);
86     assert(ret);
87     for (i = 0; i < 99 * 101; i++) {
88         assert(buffer[i] == 0);
89     }
90
91     buffer[0] = 1;
92     ret = opj_sparse_array_int32_write(sa, 4, 5, 4 + 1, 5 + 1, buffer, 1, 1,
93                                        OPJ_FALSE);
94     assert(ret);
95
96     buffer[0] = 2;
97     ret = opj_sparse_array_int32_write(sa, 4, 5, 4 + 1, 5 + 1, buffer, 1, 1,
98                                        OPJ_FALSE);
99     assert(ret);
100
101     buffer[0] = 0;
102     buffer[1] = 0xFF;
103     ret = opj_sparse_array_int32_read(sa, 4, 5, 4 + 1, 5 + 1, buffer, 1, 1,
104                                       OPJ_FALSE);
105     assert(ret);
106     assert(buffer[0] == 2);
107     assert(buffer[1] == 0xFF);
108
109     buffer[0] = 0xFF;
110     buffer[1] = 0xFF;
111     buffer[2] = 0xFF;
112     ret = opj_sparse_array_int32_read(sa, 4, 5, 4 + 1, 5 + 2, buffer, 0, 1,
113                                       OPJ_FALSE);
114     assert(ret);
115     assert(buffer[0] == 2);
116     assert(buffer[1] == 0);
117     assert(buffer[2] == 0xFF);
118
119     buffer[0] = 3;
120     ret = opj_sparse_array_int32_write(sa, 4, 5, 4 + 1, 5 + 1, buffer, 0, 1,
121                                        OPJ_FALSE);
122     assert(ret);
123
124     buffer[0] = 0;
125     buffer[1] = 0xFF;
126     ret = opj_sparse_array_int32_read(sa, 4, 5, 4 + 1, 5 + 1, buffer, 1, 1,
127                                       OPJ_FALSE);
128     assert(ret);
129     assert(buffer[0] == 3);
130     assert(buffer[1] == 0xFF);
131
132     w = 15 + 1;
133     h = 17 + 1;
134     memset(buffer, 0xFF, sizeof(buffer));
135     ret = opj_sparse_array_int32_read(sa, 2, 1, 2 + w, 1 + h, buffer, 1, w,
136                                       OPJ_FALSE);
137     assert(ret);
138     for (j = 0; j < h; j++) {
139         for (i = 0; i < w; i++) {
140             if (i == 4 - 2 && j == 5 - 1) {
141                 assert(buffer[ j * w + i ] == 3);
142             } else {
143                 assert(buffer[ j * w + i ] == 0);
144             }
145         }
146     }
147
148     opj_sparse_array_int32_free(sa);
149
150
151     sa = opj_sparse_array_int32_create(99, 101, 15, 17);
152     memset(buffer, 0xFF, sizeof(buffer));
153     ret = opj_sparse_array_int32_read(sa, 0, 0, 2, 1, buffer, 2, 4, OPJ_FALSE);
154     assert(ret);
155     assert(buffer[0] == 0);
156     assert(buffer[1] == -1);
157     assert(buffer[2] == 0);
158
159     buffer[0] = 1;
160     buffer[2] = 3;
161     ret = opj_sparse_array_int32_write(sa, 0, 0, 2, 1, buffer, 2, 4, OPJ_FALSE);
162     assert(ret);
163
164     memset(buffer, 0xFF, sizeof(buffer));
165     ret = opj_sparse_array_int32_read(sa, 0, 0, 2, 1, buffer, 2, 4, OPJ_FALSE);
166     assert(ret);
167     assert(buffer[0] == 1);
168     assert(buffer[1] == -1);
169     assert(buffer[2] == 3);
170
171     opj_sparse_array_int32_free(sa);
172
173     return 0;
174 }