user-data.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright © 2005 Red Hat, Inc.
  3. *
  4. * Permission to use, copy, modify, distribute, and sell this software
  5. * and its documentation for any purpose is hereby granted without
  6. * fee, provided that the above copyright notice appear in all copies
  7. * and that both that copyright notice and this permission notice
  8. * appear in supporting documentation, and that the name of
  9. * Red Hat, Inc. not be used in advertising or publicity pertaining to
  10. * distribution of the software without specific, written prior
  11. * permission. Red Hat, Inc. makes no representations about the
  12. * suitability of this software for any purpose. It is provided "as
  13. * is" without express or implied warranty.
  14. *
  15. * RED HAT, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
  16. * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. * FITNESS, IN NO EVENT SHALL RED HAT, INC. BE LIABLE FOR ANY SPECIAL,
  18. * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
  19. * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  20. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
  21. * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22. *
  23. * Author: Kristian Høgsberg <krh@redhat.com>
  24. */
  25. #include "cairo-test.h"
  26. #include <assert.h>
  27. static void
  28. destroy_data1 (void *p)
  29. {
  30. *(int *) p = 1;
  31. }
  32. static void
  33. destroy_data2 (void *p)
  34. {
  35. *(int *) p = 2;
  36. }
  37. static cairo_test_status_t
  38. preamble (cairo_test_context_t *ctx)
  39. {
  40. static const cairo_user_data_key_t key1, key2;
  41. cairo_surface_t *surface;
  42. cairo_status_t status;
  43. int data1, data2;
  44. data1 = 0;
  45. data2 = 0;
  46. surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 1, 1);
  47. status = cairo_surface_set_user_data (surface, &key1, &data1, destroy_data1);
  48. if (status)
  49. goto error;
  50. status = cairo_surface_set_user_data (surface, &key2, &data2, destroy_data2);
  51. if (status)
  52. goto error;
  53. assert (cairo_surface_get_user_data (surface, &key1) == &data1);
  54. status = cairo_surface_set_user_data (surface, &key1, NULL, NULL);
  55. if (status)
  56. goto error;
  57. assert (cairo_surface_get_user_data (surface, &key1) == NULL);
  58. assert (data1 == 1);
  59. assert (data2 == 0);
  60. status = cairo_surface_set_user_data (surface, &key2, NULL, NULL);
  61. if (status)
  62. goto error;
  63. assert (data2 == 2);
  64. data1 = 0;
  65. status = cairo_surface_set_user_data (surface, &key1, &data1, NULL);
  66. if (status)
  67. goto error;
  68. status = cairo_surface_set_user_data (surface, &key1, NULL, NULL);
  69. if (status)
  70. goto error;
  71. assert (data1 == 0);
  72. assert (cairo_surface_get_user_data (surface, &key1) == NULL);
  73. status = cairo_surface_set_user_data (surface, &key1, &data1, destroy_data1);
  74. if (status)
  75. goto error;
  76. cairo_surface_destroy (surface);
  77. assert (data1 == 1);
  78. assert (data2 == 2);
  79. return CAIRO_TEST_SUCCESS;
  80. error:
  81. cairo_surface_destroy (surface);
  82. return cairo_test_status_from_status (ctx, status);
  83. }
  84. CAIRO_TEST (user_data,
  85. "Test setting and getting random bits of user data.",
  86. "api", /* keywords */
  87. NULL, /* requirements */
  88. 0, 0,
  89. preamble, NULL)