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 test_format_unpack_float(const struct util_format_description
*format_desc
,
38 const struct util_format_test_case
*test
)
44 format_desc
->unpack_float(unpacked
, test
->packed
, 1);
47 for (i
= 0; i
< 4; ++i
)
48 if (test
->unpacked
[i
] != unpacked
[i
])
52 printf("FAILED: (%f %f %f %f) obtained\n", unpacked
[0], unpacked
[1], unpacked
[2], unpacked
[3]);
53 printf(" (%f %f %f %f) expected\n", test
->unpacked
[0], test
->unpacked
[1], test
->unpacked
[2], test
->unpacked
[3]);
61 test_format_pack_float(const struct util_format_description
*format_desc
,
62 const struct util_format_test_case
*test
)
65 uint8_t packed
[UTIL_FORMAT_MAX_PACKED_BYTES
];
69 memset(packed
, 0, sizeof packed
);
71 for (i
= 0; i
< 4; ++i
)
72 unpacked
[i
] = (float) test
->unpacked
[i
];
74 format_desc
->pack_float(packed
, unpacked
, 1);
77 for (i
= 0; i
< UTIL_FORMAT_MAX_PACKED_BYTES
; ++i
)
78 if ((test
->packed
[i
] & test
->mask
[i
]) != (packed
[i
] & test
->mask
[i
]))
82 /* TODO: print more than 4 bytes */
83 printf("FAILED: (%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x) obtained\n",
84 packed
[0], packed
[1], packed
[2], packed
[3],
85 packed
[4], packed
[5], packed
[6], packed
[7],
86 packed
[8], packed
[9], packed
[10], packed
[11],
87 packed
[12], packed
[13], packed
[14], packed
[15]);
88 printf(" (%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x) expected\n",
89 test
->packed
[0], test
->packed
[1], test
->packed
[2], test
->packed
[3],
90 test
->packed
[4], test
->packed
[5], test
->packed
[6], test
->packed
[7],
91 test
->packed
[8], test
->packed
[9], test
->packed
[10], test
->packed
[11],
92 test
->packed
[12], test
->packed
[13], test
->packed
[14], test
->packed
[15]);
100 convert_float_to_8unorm(uint8_t *dst
, const double *src
)
103 boolean accurate
= TRUE
;
105 for (i
= 0; i
< 4; ++i
) {
110 else if (src
[i
] > 1.0) {
115 dst
[i
] = src
[i
] * 255.0;
124 test_format_unpack_8unorm(const struct util_format_description
*format_desc
,
125 const struct util_format_test_case
*test
)
132 format_desc
->unpack_8unorm(unpacked
, test
->packed
, 1);
134 convert_float_to_8unorm(expected
, test
->unpacked
);
137 for (i
= 0; i
< 4; ++i
)
138 if (expected
[i
] != unpacked
[i
])
142 printf("FAILED: (0x%02x 0x%02x 0x%02x 0x%02x) obtained\n", unpacked
[0], unpacked
[1], unpacked
[2], unpacked
[3]);
143 printf(" (0x%02x 0x%02x 0x%02x 0x%02x) expected\n", expected
[0], expected
[1], expected
[2], expected
[3]);
151 test_format_pack_8unorm(const struct util_format_description
*format_desc
,
152 const struct util_format_test_case
*test
)
155 uint8_t packed
[UTIL_FORMAT_MAX_PACKED_BYTES
];
159 if (!convert_float_to_8unorm(unpacked
, test
->unpacked
)) {
161 * Skip test cases which cannot be represented by four unorm bytes.
166 memset(packed
, 0, sizeof packed
);
168 format_desc
->pack_8unorm(packed
, unpacked
, 1);
171 for (i
= 0; i
< UTIL_FORMAT_MAX_PACKED_BYTES
; ++i
)
172 if ((test
->packed
[i
] & test
->mask
[i
]) != (packed
[i
] & test
->mask
[i
]))
176 /* TODO: print more than 4 bytes */
177 printf("FAILED: (%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x) obtained\n",
178 packed
[0], packed
[1], packed
[2], packed
[3],
179 packed
[4], packed
[5], packed
[6], packed
[7],
180 packed
[8], packed
[9], packed
[10], packed
[11],
181 packed
[12], packed
[13], packed
[14], packed
[15]);
182 printf(" (%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x) expected\n",
183 test
->packed
[0], test
->packed
[1], test
->packed
[2], test
->packed
[3],
184 test
->packed
[4], test
->packed
[5], test
->packed
[6], test
->packed
[7],
185 test
->packed
[8], test
->packed
[9], test
->packed
[10], test
->packed
[11],
186 test
->packed
[12], test
->packed
[13], test
->packed
[14], test
->packed
[15]);
194 (*test_func_t
)(const struct util_format_description
*format_desc
,
195 const struct util_format_test_case
*test
);
199 test_one(test_func_t func
, const char *suffix
)
201 enum pipe_format last_format
= PIPE_FORMAT_NONE
;
205 for (i
= 0; i
< util_format_nr_test_cases
; ++i
) {
206 const struct util_format_test_case
*test
= &util_format_test_cases
[i
];
207 const struct util_format_description
*format_desc
;
208 format_desc
= util_format_description(test
->format
);
210 if (test
->format
!= last_format
) {
211 printf("Testing util_format_%s_%s ...\n", format_desc
->short_name
, suffix
);
212 last_format
= test
->format
;
215 if (!func(format_desc
, &util_format_test_cases
[i
]))
228 if (!test_one(&test_format_pack_float
, "pack_float"))
231 if (!test_one(&test_format_unpack_float
, "unpack_float"))
234 if (!test_one(&test_format_pack_8unorm
, "pack_8unorm"))
237 if (!test_one(&test_format_unpack_8unorm
, "unpack_8unorm"))
244 int main(int argc
, char **argv
)
248 success
= test_all();
250 return success
? 0 : 1;