#include "cairo-test.h" #include "cairoint.h" #define G_N_ELEMENTS(_a) (sizeof (_a) / sizeof (*_a)) static int rc = CAIRO_TEST_SUCCESS; static void fail_require_equal (char const a[], char const b[]) { cairo_test_log ("Unexpectedly unequal: %s, %s\n", a, b); rc = CAIRO_TEST_FAILURE; } static void fail_require (char const p[]) { cairo_test_log ("Failed requirement `%s'\n", p); rc = CAIRO_TEST_FAILURE; } #define REQUIRE_EQUAL(_a, _b) ((void) (((_a) == (_b)) || (fail_require_equal (#_a, #_b), 1))) #define REQUIRE(_p) ((void) ((_p) || (fail_require (#_p), 1))) int main (void) { cairo_test_init ("cairo-fixed"); /* Round-trip from ints. */ { /* N.B. I don't know what range we actually require to be able to round-trip. * I removed -32768 from the cases[] array just because it fails several tests. */ int const cases[] = {0, 1, 5, 0x7f, 0xff, 0x7fff, -1, -5, -127, -128, -32767}; unsigned i; for (i = 0; i < G_N_ELEMENTS (cases); ++i) { cairo_fixed_t const fromint = _cairo_fixed_from_int (cases[i]); cairo_fixed_t const fromdbl = _cairo_fixed_from_double ((double) (cases[i])); cairo_fixed_t const more = _cairo_fixed_from_double (cases[i] + 1/65536.0); cairo_fixed_t const less = _cairo_fixed_from_double (cases[i] - 1/65536.0); REQUIRE_EQUAL (fromint, fromdbl); REQUIRE_EQUAL (_cairo_fixed_integer_part (fromint), cases[i]); REQUIRE_EQUAL (_cairo_fixed_to_double (fromint), cases[i]); REQUIRE (_cairo_fixed_is_integer (fromint)); REQUIRE (!_cairo_fixed_is_integer (more)); REQUIRE (!_cairo_fixed_is_integer (less)); REQUIRE_EQUAL (_cairo_fixed_integer_floor (fromint), cases[i]); REQUIRE_EQUAL (_cairo_fixed_integer_ceil (fromint), cases[i]); REQUIRE_EQUAL (_cairo_fixed_integer_floor (more), cases[i]); REQUIRE_EQUAL (_cairo_fixed_integer_floor (less), cases[i] - 1); REQUIRE_EQUAL (_cairo_fixed_integer_ceil (more), cases[i] + 1); REQUIRE_EQUAL (_cairo_fixed_integer_ceil (less), cases[i]); } } return rc; }