76-pointer-arithmetic.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /* -*-comment-start: "//";comment-end:""-*-
  2. * GNU Mes --- Maxwell Equations of Software
  3. * Copyright © 2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  4. *
  5. * This file is part of GNU Mes.
  6. *
  7. * GNU Mes is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * GNU Mes is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <mes/lib.h>
  21. #include <stdio.h>
  22. char *list[2] = { "foo\n", "bar\n" };
  23. struct foo
  24. {
  25. int a;
  26. int b;
  27. int c;
  28. unsigned char *d;
  29. #if __MESC__ && __x86_64__
  30. int __align;
  31. #endif
  32. };
  33. int
  34. main ()
  35. {
  36. char *pc = 0;
  37. void *pv = 0;
  38. int *pi = 0;
  39. char **ppc = 0;
  40. void **ppv = 0;
  41. int **ppi = 0;
  42. int int_size = sizeof (int);
  43. int ptr_size = sizeof (void *);
  44. int foo_size = sizeof (struct foo);
  45. oputs ("int_size:");
  46. oputs (itoa (int_size));
  47. oputs ("\n");
  48. oputs ("ptr_size:");
  49. oputs (itoa (ptr_size));
  50. oputs ("\n");
  51. oputs ("foo_size:");
  52. oputs (itoa (foo_size));
  53. oputs ("\n");
  54. // FIXME: add *14, *18
  55. #if __i386__ || __arm__
  56. int foo_size_14 = 224;
  57. int foo_size_18 = 288;
  58. #elif __x86_64__
  59. int foo_size_14 = 336;
  60. int foo_size_18 = 432;
  61. #endif
  62. if (++pc != 1)
  63. return 1;
  64. if (++pv != 1)
  65. return 2;
  66. if (++pi != int_size)
  67. return 3;
  68. if (++ppc != ptr_size)
  69. return 4;
  70. if (++ppv != ptr_size)
  71. return 5;
  72. if (++ppi != ptr_size)
  73. return 6;
  74. if (pc + 1 != 2)
  75. return 7;
  76. if (pv + 1 != 2)
  77. return 8;
  78. if (pi + 1 != int_size << 1)
  79. return 9;
  80. if (ppc + 1 != ptr_size << 1)
  81. return 10;
  82. if (ppv + 1 != ptr_size << 1)
  83. return 11;
  84. if (ppi + 1 != ptr_size << 1)
  85. return 12;
  86. char **p = list;
  87. ++*p;
  88. eputs (*p);
  89. if (strcmp (*p, "oo\n"))
  90. return 13;
  91. --*p;
  92. eputs (*p);
  93. if (strcmp (*p, "foo\n"))
  94. return 14;
  95. struct foo *pfoo = 0;
  96. eputs ("pfoo=");
  97. eputs (itoa (pfoo));
  98. eputs ("\n");
  99. pfoo++;
  100. eputs ("pfoo=");
  101. eputs (itoa (pfoo));
  102. eputs ("\n");
  103. if (pfoo != foo_size)
  104. return 15;
  105. pfoo--;
  106. eputs ("pfoo=");
  107. eputs (itoa (pfoo));
  108. eputs ("\n");
  109. if (pfoo)
  110. return 16;
  111. pfoo++;
  112. eputs ("pfoo=");
  113. eputs (itoa (pfoo));
  114. eputs ("\n");
  115. if (pfoo != foo_size)
  116. return 17;
  117. long one = 1;
  118. long two = 2;
  119. pfoo = pfoo - one;
  120. eputs ("pfoo=");
  121. eputs (itoa (pfoo));
  122. eputs ("\n");
  123. if (pfoo)
  124. return 18;
  125. pfoo = pfoo + one;
  126. eputs ("pfoo=");
  127. eputs (itoa (pfoo));
  128. eputs ("\n");
  129. if (pfoo != foo_size)
  130. return 19;
  131. pfoo -= one;
  132. eputs ("pfoo=");
  133. eputs (itoa (pfoo));
  134. eputs ("\n");
  135. if (pfoo)
  136. return 20;
  137. pfoo += one;
  138. eputs ("pfoo=");
  139. eputs (itoa (pfoo));
  140. eputs ("\n");
  141. if (pfoo != foo_size)
  142. return 21;
  143. eputs ("&one: ");
  144. eputs (itoa (&one));
  145. eputs ("\n");
  146. eputs ("&two: ");
  147. eputs (itoa (&two));
  148. eputs ("\n");
  149. if (&one - 1 != &two)
  150. return 22;
  151. struct foo *sym = foo_size + foo_size;
  152. int i = sym + 16;
  153. eputs ("i=");
  154. eputs (itoa (i));
  155. eputs ("\n");
  156. if (i != foo_size_18)
  157. return 23;
  158. int d = 16;
  159. i = sym + d;
  160. eputs ("i=");
  161. eputs (itoa (i));
  162. eputs ("\n");
  163. if (i != foo_size_18)
  164. return 24;
  165. i = sym - 16;
  166. eputs ("i=");
  167. eputs (itoa (i));
  168. eputs ("\n");
  169. if (i != -foo_size_14)
  170. return 25;
  171. i = sym - d;
  172. eputs ("i=");
  173. eputs (itoa (i));
  174. eputs ("\n");
  175. if (i != -foo_size_14)
  176. return 26;
  177. i = sym - (struct foo *) foo_size;
  178. eputs ("i=");
  179. eputs (itoa (i));
  180. eputs ("\n");
  181. if (i != 1)
  182. return 27;
  183. pfoo = sym + 1;
  184. pfoo -= sym;
  185. eputs ("pfoo=");
  186. eputs (itoa (pfoo));
  187. eputs ("\n");
  188. if (pfoo != 1)
  189. return 28;
  190. return 0;
  191. }