sizes.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Tests to ensure that our type definitions are correct
  3. *
  4. * These depend on -Werror, -Wall being set to catch the build error.
  5. */
  6. #include <stdio.h>
  7. #ifndef _MSC_VER
  8. #include <stdint.h>
  9. #endif
  10. #include <string.h>
  11. #include <glib.h>
  12. #include "test.h"
  13. RESULT
  14. test_formats ()
  15. {
  16. char buffer [1024];
  17. gsize a = 1;
  18. sprintf (buffer, "%" G_GSIZE_FORMAT, a);
  19. return NULL;
  20. }
  21. RESULT
  22. test_ptrconv ()
  23. {
  24. int iv, iv2;
  25. unsigned int uv, uv2;
  26. gpointer ptr;
  27. iv = G_MAXINT32;
  28. ptr = GINT_TO_POINTER (iv);
  29. iv2 = GPOINTER_TO_INT (ptr);
  30. if (iv != iv2)
  31. return FAILED ("int to pointer and back conversions fail %d != %d", iv, iv2);
  32. iv = G_MININT32;
  33. ptr = GINT_TO_POINTER (iv);
  34. iv2 = GPOINTER_TO_INT (ptr);
  35. if (iv != iv2)
  36. return FAILED ("int to pointer and back conversions fail %d != %d", iv, iv2);
  37. iv = 1;
  38. ptr = GINT_TO_POINTER (iv);
  39. iv2 = GPOINTER_TO_INT (ptr);
  40. if (iv != iv2)
  41. return FAILED ("int to pointer and back conversions fail %d != %d", iv, iv2);
  42. iv = -1;
  43. ptr = GINT_TO_POINTER (iv);
  44. iv2 = GPOINTER_TO_INT (ptr);
  45. if (iv != iv2)
  46. return FAILED ("int to pointer and back conversions fail %d != %d", iv, iv2);
  47. iv = 0;
  48. ptr = GINT_TO_POINTER (iv);
  49. iv2 = GPOINTER_TO_INT (ptr);
  50. if (iv != iv2)
  51. return FAILED ("int to pointer and back conversions fail %d != %d", iv, iv2);
  52. uv = 0;
  53. ptr = GUINT_TO_POINTER (iv);
  54. uv2 = GPOINTER_TO_UINT (ptr);
  55. if (iv != iv2)
  56. return FAILED ("uint to pointer and back conversions fail %u != %d", iv, iv2);
  57. uv = 1;
  58. ptr = GUINT_TO_POINTER (iv);
  59. uv2 = GPOINTER_TO_UINT (ptr);
  60. if (iv != iv2)
  61. return FAILED ("uint to pointer and back conversions fail %u != %d", iv, iv2);
  62. uv = UINT32_MAX;
  63. ptr = GUINT_TO_POINTER (iv);
  64. uv2 = GPOINTER_TO_UINT (ptr);
  65. if (iv != iv2)
  66. return FAILED ("uint to pointer and back conversions fail %u != %d", iv, iv2);
  67. return NULL;
  68. }
  69. typedef struct {
  70. int a;
  71. int b;
  72. } my_struct;
  73. RESULT
  74. test_offset ()
  75. {
  76. if (G_STRUCT_OFFSET (my_struct, a) != 0)
  77. return FAILED ("offset of a is not zero");
  78. if (G_STRUCT_OFFSET (my_struct, b) != 4 && G_STRUCT_OFFSET (my_struct, b) != 8)
  79. return FAILED ("offset of b is 4 or 8, macro might be busted");
  80. return OK;
  81. }
  82. static Test size_tests [] = {
  83. {"formats", test_formats},
  84. {"ptrconv", test_ptrconv},
  85. {"g_struct_offset", test_offset},
  86. {NULL, NULL}
  87. };
  88. DEFINE_TEST_GROUP_INIT(size_tests_init, size_tests)