1 /**************************************************************************
3 * Copyright 2009-2010 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
32 #include "util/u_format.h"
33 #include "util/u_format_tests.h"
37 print_packed(const struct util_format_description
*format_desc
,
39 const uint8_t *packed
,
46 for (i
= 0; i
< format_desc
->block
.bits
/8; ++i
) {
47 printf("%s%02x", sep
, packed
[i
]);
55 print_unpacked_doubl(const struct util_format_description
*format_desc
,
57 const double unpacked
[UTIL_FORMAT_MAX_UNPACKED_HEIGHT
][UTIL_FORMAT_MAX_UNPACKED_WIDTH
][4],
64 for (i
= 0; i
< format_desc
->block
.height
; ++i
) {
65 for (j
= 0; j
< format_desc
->block
.width
; ++j
) {
66 printf("%s{%f, %f, %f, %f}", sep
, unpacked
[i
][j
][0], unpacked
[i
][j
][1], unpacked
[i
][j
][2], unpacked
[i
][j
][3]);
76 print_unpacked_float(const struct util_format_description
*format_desc
,
78 const float unpacked
[UTIL_FORMAT_MAX_UNPACKED_HEIGHT
][UTIL_FORMAT_MAX_UNPACKED_WIDTH
][4],
85 for (i
= 0; i
< format_desc
->block
.height
; ++i
) {
86 for (j
= 0; j
< format_desc
->block
.width
; ++j
) {
87 printf("%s{%f, %f, %f, %f}", sep
, unpacked
[i
][j
][0], unpacked
[i
][j
][1], unpacked
[i
][j
][2], unpacked
[i
][j
][3]);
97 print_unpacked_8unorm(const struct util_format_description
*format_desc
,
99 const uint8_t unpacked
[][UTIL_FORMAT_MAX_UNPACKED_WIDTH
][4],
103 const char *sep
= "";
105 printf("%s", prefix
);
106 for (i
= 0; i
< format_desc
->block
.height
; ++i
) {
107 for (j
= 0; j
< format_desc
->block
.width
; ++j
) {
108 printf("%s{0x%02x, 0x%02x, 0x%02x, 0x%02x}", sep
, unpacked
[i
][j
][0], unpacked
[i
][j
][1], unpacked
[i
][j
][2], unpacked
[i
][j
][3]);
112 printf("%s", suffix
);
117 test_format_fetch_float(const struct util_format_description
*format_desc
,
118 const struct util_format_test_case
*test
)
120 float unpacked
[UTIL_FORMAT_MAX_UNPACKED_HEIGHT
][UTIL_FORMAT_MAX_UNPACKED_WIDTH
][4];
125 for (i
= 0; i
< format_desc
->block
.height
; ++i
) {
126 for (j
= 0; j
< format_desc
->block
.width
; ++j
) {
127 format_desc
->fetch_float(unpacked
[i
][j
], test
->packed
, j
, i
);
128 for (k
= 0; k
< 4; ++k
) {
129 if (test
->unpacked
[i
][j
][k
] != unpacked
[i
][j
][k
]) {
137 print_unpacked_float(format_desc
, "FAILED: ", unpacked
, " obtained\n");
138 print_unpacked_doubl(format_desc
, " ", test
->unpacked
, " expected\n");
146 test_format_unpack_float(const struct util_format_description
*format_desc
,
147 const struct util_format_test_case
*test
)
149 float unpacked
[UTIL_FORMAT_MAX_UNPACKED_HEIGHT
][UTIL_FORMAT_MAX_UNPACKED_WIDTH
][4];
153 format_desc
->unpack_float(&unpacked
[0][0][0], sizeof unpacked
[0], test
->packed
, 0, format_desc
->block
.width
, format_desc
->block
.height
);
156 for (i
= 0; i
< format_desc
->block
.height
; ++i
) {
157 for (j
= 0; j
< format_desc
->block
.width
; ++j
) {
158 for (k
= 0; k
< 4; ++k
) {
159 if (test
->unpacked
[i
][j
][k
] != unpacked
[i
][j
][k
]) {
167 print_unpacked_float(format_desc
, "FAILED: ", unpacked
, " obtained\n");
168 print_unpacked_doubl(format_desc
, " ", test
->unpacked
, " expected\n");
177 test_format_pack_float(const struct util_format_description
*format_desc
,
178 const struct util_format_test_case
*test
)
180 float unpacked
[UTIL_FORMAT_MAX_UNPACKED_HEIGHT
][UTIL_FORMAT_MAX_UNPACKED_WIDTH
][4];
181 uint8_t packed
[UTIL_FORMAT_MAX_PACKED_BYTES
];
185 memset(packed
, 0, sizeof packed
);
186 for (i
= 0; i
< format_desc
->block
.height
; ++i
) {
187 for (j
= 0; j
< format_desc
->block
.width
; ++j
) {
188 for (k
= 0; k
< 4; ++k
) {
189 unpacked
[i
][j
][k
] = (float) test
->unpacked
[i
][j
][k
];
194 format_desc
->pack_float(packed
, 0, &unpacked
[0][0][0], sizeof unpacked
[0], format_desc
->block
.width
, format_desc
->block
.height
);
197 for (i
= 0; i
< format_desc
->block
.bits
/8; ++i
)
198 if ((test
->packed
[i
] & test
->mask
[i
]) != (packed
[i
] & test
->mask
[i
]))
202 print_packed(format_desc
, "FAILED: ", packed
, " obtained\n");
203 print_packed(format_desc
, " ", test
->packed
, " expected\n");
211 convert_float_to_8unorm(uint8_t *dst
, const double *src
)
214 boolean accurate
= TRUE
;
216 for (i
= 0; i
< UTIL_FORMAT_MAX_UNPACKED_HEIGHT
*UTIL_FORMAT_MAX_UNPACKED_WIDTH
*4; ++i
) {
221 else if (src
[i
] > 1.0) {
226 dst
[i
] = src
[i
] * 255.0;
235 test_format_unpack_8unorm(const struct util_format_description
*format_desc
,
236 const struct util_format_test_case
*test
)
238 uint8_t unpacked
[UTIL_FORMAT_MAX_UNPACKED_HEIGHT
][UTIL_FORMAT_MAX_UNPACKED_WIDTH
][4];
239 uint8_t expected
[UTIL_FORMAT_MAX_UNPACKED_HEIGHT
][UTIL_FORMAT_MAX_UNPACKED_WIDTH
][4];
243 format_desc
->unpack_8unorm(&unpacked
[0][0][0], 0, test
->packed
, 0, 1, 1);
245 convert_float_to_8unorm(&expected
[0][0][0], &test
->unpacked
[0][0][0]);
248 for (i
= 0; i
< format_desc
->block
.height
; ++i
) {
249 for (j
= 0; j
< format_desc
->block
.width
; ++j
) {
250 for (k
= 0; k
< 4; ++k
) {
251 if (expected
[i
][j
][k
] != unpacked
[i
][j
][k
]) {
259 print_unpacked_8unorm(format_desc
, "FAILED: ", unpacked
, " obtained\n");
260 print_unpacked_8unorm(format_desc
, " ", expected
, " expected\n");
268 test_format_pack_8unorm(const struct util_format_description
*format_desc
,
269 const struct util_format_test_case
*test
)
271 uint8_t unpacked
[UTIL_FORMAT_MAX_UNPACKED_HEIGHT
][UTIL_FORMAT_MAX_UNPACKED_WIDTH
][4];
272 uint8_t packed
[UTIL_FORMAT_MAX_PACKED_BYTES
];
276 if (!convert_float_to_8unorm(&unpacked
[0][0][0], &test
->unpacked
[0][0][0])) {
278 * Skip test cases which cannot be represented by four unorm bytes.
283 memset(packed
, 0, sizeof packed
);
285 format_desc
->pack_8unorm(packed
, 0, &unpacked
[0][0][0], sizeof unpacked
[0], 1, 1);
288 for (i
= 0; i
< format_desc
->block
.bits
/8; ++i
)
289 if ((test
->packed
[i
] & test
->mask
[i
]) != (packed
[i
] & test
->mask
[i
]))
293 print_packed(format_desc
, "FAILED: ", packed
, " obtained\n");
294 print_packed(format_desc
, " ", test
->packed
, " expected\n");
302 (*test_func_t
)(const struct util_format_description
*format_desc
,
303 const struct util_format_test_case
*test
);
307 test_one(test_func_t func
, const char *suffix
)
309 enum pipe_format last_format
= PIPE_FORMAT_NONE
;
313 for (i
= 0; i
< util_format_nr_test_cases
; ++i
) {
314 const struct util_format_test_case
*test
= &util_format_test_cases
[i
];
315 const struct util_format_description
*format_desc
;
316 format_desc
= util_format_description(test
->format
);
318 if (test
->format
!= last_format
) {
319 printf("Testing util_format_%s_%s ...\n", format_desc
->short_name
, suffix
);
320 last_format
= test
->format
;
323 if (!func(format_desc
, &util_format_test_cases
[i
]))
336 if (!test_one(&test_format_fetch_float
, "fetch_float"))
339 if (!test_one(&test_format_pack_float
, "pack_float"))
342 if (!test_one(&test_format_unpack_float
, "unpack_float"))
345 if (!test_one(&test_format_pack_8unorm
, "pack_8unorm"))
348 if (!test_one(&test_format_unpack_8unorm
, "unpack_8unorm"))
355 int main(int argc
, char **argv
)
359 success
= test_all();
361 return success
? 0 : 1;