Fix plugins with MSVC, thanks to Gulam Faruque.
[syx.git] / tests / testforeignstruct.c
blob33f63cee40911d6ef2206d79a5e9139b919d0a38
1 /*
2 Copyright (c) 2007-2008 Luca Bruno
4 This file is part of Smalltalk YX.
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 DEALINGS IN THE SOFTWARE.
25 #include "../syx/syx.h"
27 #include <assert.h>
28 #include <stdio.h>
30 struct _test1
32 char f1;
33 char f2;
34 char f3;
35 char f4;
36 int f5;
37 long f6;
38 } test1;
40 struct _test2
42 char f1;
43 char f2;
44 short int f3;
45 short int f4;
46 int f5;
47 int f6;
48 void *f7;
49 void *f8;
50 char f9;
51 int f10;
52 char f11;
53 void *f12;
54 short int f13;
55 void *f14;
56 } test2;
58 struct _test3
60 short int f1;
61 int f2;
62 char f3;
63 double f4;
64 float f5;
65 long f6;
66 } test3;
68 union _test4
70 char f1;
71 short int f2;
72 int f3;
73 long f4;
74 } test4;
76 int SYX_CDECL
77 main (int argc, char *argv[])
79 SyxOop process, context;
80 SyxOop arguments;
81 SyxOop structTestClass;
82 SyxOop structTest;
84 syx_init (0, NULL, "..");
85 syx_memory_load_image ("test.sim");
86 syx_scheduler_init ();
88 puts ("- Initializing test classes");
89 syx_cold_file_in ("stsupport/TestCStruct.st");
90 structTestClass = syx_globals_at ("TestCStruct");
91 syx_object_initialize (structTestClass);
92 structTest = syx_object_new (structTestClass);
94 puts ("- Test fields alignment 1");
95 arguments = syx_array_new_size (6);
96 SYX_OBJECT_DATA(arguments)[0] = syx_small_integer_new ((syx_nint)&test1.f1 - (syx_nint)&test1);
97 SYX_OBJECT_DATA(arguments)[1] = syx_small_integer_new ((syx_nint)&test1.f2 - (syx_nint)&test1);
98 SYX_OBJECT_DATA(arguments)[2] = syx_small_integer_new ((syx_nint)&test1.f3 - (syx_nint)&test1);
99 SYX_OBJECT_DATA(arguments)[3] = syx_small_integer_new ((syx_nint)&test1.f4 - (syx_nint)&test1);
100 SYX_OBJECT_DATA(arguments)[4] = syx_small_integer_new ((syx_nint)&test1.f5 - (syx_nint)&test1);
101 SYX_OBJECT_DATA(arguments)[5] = syx_small_integer_new ((syx_nint)&test1.f6 - (syx_nint)&test1);
102 process = syx_process_new ();
103 context = syx_send_message (structTest,
104 "testOffsets:from:",
105 2, arguments, syx_symbol_new ("Test1Struct"));
106 syx_interp_enter_context (process, context);
107 syx_process_execute_blocking (process);
109 puts ("- Test fields alignment 2");
110 arguments = syx_array_new_size (14);
111 SYX_OBJECT_DATA(arguments)[0] = syx_small_integer_new ((syx_nint)&test2.f1 - (syx_nint)&test2);
112 SYX_OBJECT_DATA(arguments)[1] = syx_small_integer_new ((syx_nint)&test2.f2 - (syx_nint)&test2);
113 SYX_OBJECT_DATA(arguments)[2] = syx_small_integer_new ((syx_nint)&test2.f3 - (syx_nint)&test2);
114 SYX_OBJECT_DATA(arguments)[3] = syx_small_integer_new ((syx_nint)&test2.f4 - (syx_nint)&test2);
115 SYX_OBJECT_DATA(arguments)[4] = syx_small_integer_new ((syx_nint)&test2.f5 - (syx_nint)&test2);
116 SYX_OBJECT_DATA(arguments)[5] = syx_small_integer_new ((syx_nint)&test2.f6 - (syx_nint)&test2);
117 SYX_OBJECT_DATA(arguments)[6] = syx_small_integer_new ((syx_nint)&test2.f7 - (syx_nint)&test2);
118 SYX_OBJECT_DATA(arguments)[7] = syx_small_integer_new ((syx_nint)&test2.f8 - (syx_nint)&test2);
119 SYX_OBJECT_DATA(arguments)[8] = syx_small_integer_new ((syx_nint)&test2.f9 - (syx_nint)&test2);
120 SYX_OBJECT_DATA(arguments)[9] = syx_small_integer_new ((syx_nint)&test2.f10 - (syx_nint)&test2);
121 SYX_OBJECT_DATA(arguments)[10] = syx_small_integer_new ((syx_nint)&test2.f11 - (syx_nint)&test2);
122 SYX_OBJECT_DATA(arguments)[11] = syx_small_integer_new ((syx_nint)&test2.f12 - (syx_nint)&test2);
123 SYX_OBJECT_DATA(arguments)[12] = syx_small_integer_new ((syx_nint)&test2.f13 - (syx_nint)&test2);
124 SYX_OBJECT_DATA(arguments)[13] = syx_small_integer_new ((syx_nint)&test2.f14 - (syx_nint)&test2);
125 process = syx_process_new ();
126 context = syx_send_message (structTest,
127 "testOffsets:from:",
128 2, arguments, syx_symbol_new ("Test2Struct"));
129 syx_interp_enter_context (process, context);
130 syx_process_execute_blocking (process);
132 puts ("- Test fields alignment 3");
133 arguments = syx_array_new_size (6);
134 SYX_OBJECT_DATA(arguments)[0] = syx_small_integer_new ((syx_nint)&test3.f1 - (syx_nint)&test3);
135 SYX_OBJECT_DATA(arguments)[1] = syx_small_integer_new ((syx_nint)&test3.f2 - (syx_nint)&test3);
136 SYX_OBJECT_DATA(arguments)[2] = syx_small_integer_new ((syx_nint)&test3.f3 - (syx_nint)&test3);
137 SYX_OBJECT_DATA(arguments)[3] = syx_small_integer_new ((syx_nint)&test3.f4 - (syx_nint)&test3);
138 SYX_OBJECT_DATA(arguments)[4] = syx_small_integer_new ((syx_nint)&test3.f5 - (syx_nint)&test3);
139 SYX_OBJECT_DATA(arguments)[5] = syx_small_integer_new ((syx_nint)&test3.f6 - (syx_nint)&test3);
140 process = syx_process_new ();
141 context = syx_send_message (structTest,
142 "testOffsets:from:",
143 2, arguments, syx_symbol_new ("Test3Struct"));
144 syx_interp_enter_context (process, context);
145 syx_process_execute_blocking (process);
147 puts ("- Test reading");
148 test3.f1 = 240;
149 test3.f2 = 7143;
150 test3.f3 = 'R';
151 test3.f4 = 199.11822;
152 test3.f5 = 23.5; /* won't read yet */
153 process = syx_process_new ();
154 context = syx_send_binary_message (structTest,
155 "testRead:",
156 SYX_POINTER_CAST_OOP (&test3));
157 syx_interp_enter_context (process, context);
158 syx_process_execute_blocking (process);
160 puts ("- Test writing");
161 process = syx_process_new ();
162 context = syx_send_binary_message (structTest,
163 "testWrite:",
164 SYX_POINTER_CAST_OOP (&test3));
165 syx_interp_enter_context (process, context);
166 syx_process_execute_blocking (process);
167 assert(test3.f1 == 320);
168 assert(test3.f2 == 10293);
169 assert(test3.f3 == ',');
170 assert(test3.f4 == 291.4837);
171 /* Double to float conversion
172 assert(test3.f5 == 76.119);
174 /* won't write longs yet */
176 puts ("- Test unions");
177 test4.f1 = 'S';
178 process = syx_process_new ();
179 context = syx_send_binary_message (structTest,
180 "testUnion:",
181 SYX_POINTER_CAST_OOP (&test4));
182 syx_interp_enter_context (process, context);
183 syx_process_execute_blocking (process);
184 assert(test4.f1 == 'T');
186 syx_quit ();
187 return 0;