-
Notifications
You must be signed in to change notification settings - Fork 123
/
test.go
2297 lines (1892 loc) · 47.4 KB
/
test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Test cases for cgo.
// Both the import "C" prologue and the main file are sorted by issue number.
// This file contains C definitions (not just declarations)
// and so it must NOT contain any //export directives on Go functions.
// See testx.go for exports.
package cgotest
/*
#include <complex.h>
#include <math.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <errno.h>
#cgo LDFLAGS: -lm
#ifndef WIN32
#include <pthread.h>
#include <signal.h>
#endif
// alignment tests
typedef unsigned char Uint8;
typedef unsigned short Uint16;
typedef enum {
MOD1 = 0x0000,
MODX = 0x8000
} SDLMod;
typedef enum {
A1 = 1,
B1 = 322,
SDLK_LAST
} SDLKey;
typedef struct SDL_keysym {
Uint8 scancode;
SDLKey sym;
SDLMod mod;
Uint16 unicode;
} SDL_keysym;
typedef struct SDL_KeyboardEvent {
Uint8 typ;
Uint8 which;
Uint8 state;
SDL_keysym keysym;
} SDL_KeyboardEvent;
void makeEvent(SDL_KeyboardEvent *event) {
unsigned char *p;
int i;
p = (unsigned char*)event;
for (i=0; i<sizeof *event; i++) {
p[i] = i;
}
}
int same(SDL_KeyboardEvent* e, Uint8 typ, Uint8 which, Uint8 state, Uint8 scan, SDLKey sym, SDLMod mod, Uint16 uni) {
return e->typ == typ && e->which == which && e->state == state && e->keysym.scancode == scan && e->keysym.sym == sym && e->keysym.mod == mod && e->keysym.unicode == uni;
}
void cTest(SDL_KeyboardEvent *event) {
printf("C: %#x %#x %#x %#x %#x %#x %#x\n", event->typ, event->which, event->state,
event->keysym.scancode, event->keysym.sym, event->keysym.mod, event->keysym.unicode);
fflush(stdout);
}
// api
const char *greeting = "hello, world";
// basic test cases
#define SHIFT(x, y) ((x)<<(y))
#define KILO SHIFT(1, 10)
#define UINT32VAL 0xc008427bU
enum E {
Enum1 = 1,
Enum2 = 2,
};
typedef unsigned char cgo_uuid_t[20];
void uuid_generate(cgo_uuid_t x) {
x[0] = 0;
}
struct S {
int x;
};
const char *cstr = "abcefghijklmnopqrstuvwxyzABCEFGHIJKLMNOPQRSTUVWXYZ1234567890";
extern enum E myConstFunc(struct S* const ctx, int const id, struct S **const filter);
enum E myConstFunc(struct S *const ctx, int const id, struct S **const filter) { return 0; }
int add(int x, int y) {
return x+y;
};
// Following mimicks vulkan complex definitions for benchmarking cgocheck overhead.
typedef uint32_t VkFlags;
typedef VkFlags VkDeviceQueueCreateFlags;
typedef uint32_t VkStructureType;
typedef struct VkDeviceQueueCreateInfo {
VkStructureType sType;
const void* pNext;
VkDeviceQueueCreateFlags flags;
uint32_t queueFamilyIndex;
uint32_t queueCount;
const float* pQueuePriorities;
} VkDeviceQueueCreateInfo;
typedef struct VkPhysicalDeviceFeatures {
uint32_t bools[56];
} VkPhysicalDeviceFeatures;
typedef struct VkDeviceCreateInfo {
VkStructureType sType;
const void* pNext;
VkFlags flags;
uint32_t queueCreateInfoCount;
const VkDeviceQueueCreateInfo* pQueueCreateInfos;
uint32_t enabledLayerCount;
const char* const* ppEnabledLayerNames;
uint32_t enabledExtensionCount;
const char* const* ppEnabledExtensionNames;
const VkPhysicalDeviceFeatures* pEnabledFeatures;
} VkDeviceCreateInfo;
void handleComplexPointer(VkDeviceCreateInfo *a0) {}
void handleComplexPointer8(
VkDeviceCreateInfo *a0, VkDeviceCreateInfo *a1, VkDeviceCreateInfo *a2, VkDeviceCreateInfo *a3,
VkDeviceCreateInfo *a4, VkDeviceCreateInfo *a5, VkDeviceCreateInfo *a6, VkDeviceCreateInfo *a7
) {}
// complex alignment
struct {
float x;
_Complex float y;
} cplxAlign = { 3.14, 2.17 };
// constants and pointer checking
#define CheckConstVal 0
typedef struct {
int *p;
} CheckConstStruct;
static void CheckConstFunc(CheckConstStruct *p, int e) {}
// duplicate symbol
int base_symbol = 0;
#define alias_one base_symbol
#define alias_two base_symbol
// function pointer variables
typedef int (*intFunc) ();
int
bridge_int_func(intFunc f)
{
return f();
}
int fortytwo()
{
return 42;
}
// issue 1222
typedef union {
long align;
} xxpthread_mutex_t;
struct ibv_async_event {
union {
int x;
} element;
};
struct ibv_context {
xxpthread_mutex_t mutex;
};
// issue 1635
// Mac OS X's gcc will generate scattered relocation 2/1 for
// this function on Darwin/386, and 8l couldn't handle it.
// this example is in issue 1635
void scatter() {
void *p = scatter;
printf("scatter = %p\n", p);
}
// Adding this explicit extern declaration makes this a test for
// https://gcc.gnu.org/PR68072 aka https://golang.org/issue/13344 .
// It used to cause a cgo error when building with GCC 6.
extern int hola;
// this example is in issue 3253
int hola = 0;
int testHola() { return hola; }
// issue 3250
#ifdef WIN32
void testSendSIG() {}
#else
static void *thread(void *p) {
const int M = 100;
int i;
(void)p;
for (i = 0; i < M; i++) {
pthread_kill(pthread_self(), SIGCHLD);
usleep(rand() % 20 + 5);
}
return NULL;
}
void testSendSIG() {
const int N = 20;
int i;
pthread_t tid[N];
for (i = 0; i < N; i++) {
usleep(rand() % 200 + 100);
pthread_create(&tid[i], 0, thread, NULL);
}
for (i = 0; i < N; i++)
pthread_join(tid[i], 0);
}
#endif
// issue 3261
// libgcc on ARM might be compiled as thumb code, but our 5l
// can't handle that, so we have to disable this test on arm.
#ifdef __ARMEL__
int vabs(int x) {
puts("testLibgcc is disabled on ARM because 5l cannot handle thumb library.");
return (x < 0) ? -x : x;
}
#elif defined(__arm64__) && defined(__clang__)
int vabs(int x) {
puts("testLibgcc is disabled on ARM64 with clang due to lack of libgcc.");
return (x < 0) ? -x : x;
}
#else
int __absvsi2(int); // dummy prototype for libgcc function
// we shouldn't name the function abs, as gcc might use
// the builtin one.
int vabs(int x) { return __absvsi2(x); }
#endif
// issue 3729
// access errno from void C function
const char _expA = 0x42;
const float _expB = 3.14159;
const short _expC = 0x55aa;
const int _expD = 0xdeadbeef;
#ifdef WIN32
void g(void) {}
void g2(int x, char a, float b, short c, int d) {}
#else
void g(void) {
errno = E2BIG;
}
// try to pass some non-trivial arguments to function g2
void g2(int x, char a, float b, short c, int d) {
if (a == _expA && b == _expB && c == _expC && d == _expD)
errno = x;
else
errno = -1;
}
#endif
// issue 3945
// Test that cgo reserves enough stack space during cgo call.
// See https://golang.org/issue/3945 for details.
void say() {
printf("%s from C\n", "hello");
}
// issue 4054 part 1 - other half in testx.go
typedef enum {
A = 0,
B,
C,
D,
E,
F,
G,
H,
II,
J,
} issue4054a;
// issue 4339
// We've historically permitted #include <>, so test it here. Issue 29333.
// Also see issue 41059.
#include <issue4339.h>
// issue 4417
// cmd/cgo: bool alignment/padding issue.
// bool alignment is wrong and causing wrong arguments when calling functions.
static int c_bool(bool a, bool b, int c, bool d, bool e) {
return c;
}
// issue 4857
#cgo CFLAGS: -Werror
const struct { int a; } *issue4857() { return (void *)0; }
// issue 5224
// Test that the #cgo CFLAGS directive works,
// with and without platform filters.
#cgo CFLAGS: -DCOMMON_VALUE=123
#cgo windows CFLAGS: -DIS_WINDOWS=1
#cgo !windows CFLAGS: -DIS_WINDOWS=0
int common = COMMON_VALUE;
int is_windows = IS_WINDOWS;
// issue 5227
// linker incorrectly treats common symbols and
// leaves them undefined.
typedef struct {
int Count;
} Fontinfo;
Fontinfo SansTypeface;
extern void init();
Fontinfo loadfont() {
Fontinfo f = {0};
return f;
}
void init() {
SansTypeface = loadfont();
}
// issue 5242
// Cgo incorrectly computed the alignment of structs
// with no Go accessible fields as 0, and then panicked on
// modulo-by-zero computations.
// issue 50987
// disable arm64 GCC warnings
#cgo CFLAGS: -Wno-psabi -Wno-unknown-warning-option
typedef struct {
} foo;
typedef struct {
int x : 1;
} bar;
int issue5242(foo f, bar b) {
return 5242;
}
// issue 5337
// Verify that we can withstand SIGPROF received on foreign threads
#ifdef WIN32
void test5337() {}
#else
static void *thread1(void *p) {
(void)p;
pthread_kill(pthread_self(), SIGPROF);
return NULL;
}
void test5337() {
pthread_t tid;
pthread_create(&tid, 0, thread1, NULL);
pthread_join(tid, 0);
}
#endif
// issue 5603
const long long issue5603exp = 0x12345678;
long long issue5603foo0() { return issue5603exp; }
long long issue5603foo1(void *p) { return issue5603exp; }
long long issue5603foo2(void *p, void *q) { return issue5603exp; }
long long issue5603foo3(void *p, void *q, void *r) { return issue5603exp; }
long long issue5603foo4(void *p, void *q, void *r, void *s) { return issue5603exp; }
// issue 5740
int test5740a(void), test5740b(void);
// issue 5986
static void output5986()
{
int current_row = 0, row_count = 0;
double sum_squares = 0;
double d;
do {
if (current_row == 10) {
current_row = 0;
}
++row_count;
}
while (current_row++ != 1);
d = sqrt(sum_squares / row_count);
printf("sqrt is: %g\n", d);
}
// issue 6128
// Test handling of #defined names in clang.
// NOTE: Must use hex, or else a shortcut for decimals
// in cgo avoids trying to pass this to clang.
#define X 0x1
// issue 6472
typedef struct
{
struct
{
int x;
} y[16];
} z;
// issue 6612
// Test new scheme for deciding whether C.name is an expression, type, constant.
// Clang silences some warnings when the name is a #defined macro, so test those too
// (even though we now use errors exclusively, not warnings).
void myfunc(void) {}
int myvar = 5;
const char *mytext = "abcdef";
typedef int mytype;
enum {
myenum = 1234,
};
#define myfunc_def myfunc
#define myvar_def myvar
#define mytext_def mytext
#define mytype_def mytype
#define myenum_def myenum
#define myint_def 12345
#define myfloat_def 1.5
#define mystring_def "hello"
// issue 6907
char* Issue6907CopyString(_GoString_ s) {
size_t n;
const char *p;
char *r;
n = _GoStringLen(s);
p = _GoStringPtr(s);
r = malloc(n + 1);
memmove(r, p, n);
r[n] = '\0';
return r;
}
// issue 7560
typedef struct {
char x;
long y;
} __attribute__((__packed__)) misaligned;
int
offset7560(void)
{
return (uintptr_t)&((misaligned*)0)->y;
}
// issue 7786
// No runtime test, just make sure that typedef and struct/union/class are interchangeable at compile time.
struct test7786;
typedef struct test7786 typedef_test7786;
void f7786(struct test7786 *ctx) {}
void g7786(typedef_test7786 *ctx) {}
typedef struct body7786 typedef_body7786;
struct body7786 { int x; };
void b7786(struct body7786 *ctx) {}
void c7786(typedef_body7786 *ctx) {}
typedef union union7786 typedef_union7786;
void u7786(union union7786 *ctx) {}
void v7786(typedef_union7786 *ctx) {}
// issue 8092
// Test that linker defined symbols (e.g., text, data) don't
// conflict with C symbols.
char text[] = "text";
char data[] = "data";
char *ctext(void) { return text; }
char *cdata(void) { return data; }
// issue 8428
// Cgo inconsistently translated zero size arrays.
struct issue8428one {
char b;
char rest[];
};
struct issue8428two {
void *p;
char b;
char rest[0];
char pad;
};
struct issue8428three {
char w[1][2][3][0];
char x[2][3][0][1];
char y[3][0][1][2];
char z[0][1][2][3];
};
// issue 8331 part 1 - part 2 in testx.go
// A typedef of an unnamed struct is the same struct when
// #include'd twice. No runtime test; just make sure it compiles.
#include "issue8331.h"
// issue 8368 and 8441
// Recursive struct definitions didn't work.
// No runtime test; just make sure it compiles.
typedef struct one one;
typedef struct two two;
struct one {
two *x;
};
struct two {
one *x;
};
// issue 8811
extern int issue8811Initialized;
extern void issue8811Init();
void issue8811Execute() {
if(!issue8811Initialized)
issue8811Init();
}
// issue 8945
typedef void (*PFunc8945)();
PFunc8945 func8945;
// issue 9557
struct issue9557_t {
int a;
} test9557bar = { 42 };
struct issue9557_t *issue9557foo = &test9557bar;
// issue 10303
// Pointers passed to C were not marked as escaping (bug in cgo).
typedef int *intptr;
void setintstar(int *x) {
*x = 1;
}
void setintptr(intptr x) {
*x = 1;
}
void setvoidptr(void *x) {
*(int*)x = 1;
}
typedef struct Struct Struct;
struct Struct {
int *P;
};
void setstruct(Struct s) {
*s.P = 1;
}
// issue 11925
// Structs with zero-length trailing fields are now padded by the Go compiler.
struct a11925 {
int i;
char a[0];
char b[0];
};
struct b11925 {
int i;
char a[0];
char b[];
};
// issue 12030
void issue12030conv(char *buf, double x) {
sprintf(buf, "d=%g", x);
}
// issue 14838
int check_cbytes(char *b, size_t l) {
int i;
for (i = 0; i < l; i++) {
if (b[i] != i) {
return 0;
}
}
return 1;
}
// issue 17065
// Test that C symbols larger than a page play nicely with the race detector.
int ii[65537];
// issue 17537
// The void* cast introduced by cgo to avoid problems
// with const/volatile qualifiers breaks C preprocessor macros that
// emulate functions.
typedef struct {
int i;
} S17537;
int I17537(S17537 *p);
#define I17537(p) ((p)->i)
// Calling this function used to fail without the cast.
const int F17537(const char **p) {
return **p;
}
// issue 17723
// API compatibility checks
typedef char *cstring_pointer;
static void cstring_pointer_fun(cstring_pointer dummy) { }
const char *api_hello = "hello!";
// Calling this function used to trigger an error from the C compiler
// (issue 18298).
void F18298(const void *const *p) {
}
// Test that conversions between typedefs work as they used to.
typedef const void *T18298_1;
struct S18298 { int i; };
typedef const struct S18298 *T18298_2;
void G18298(T18298_1 t) {
}
// issue 18126
// cgo check of void function returning errno.
void Issue18126C(void **p) {}
// issue 18720
#define HELLO "hello"
#define WORLD "world"
#define HELLO_WORLD HELLO "\000" WORLD
struct foo { char c; };
#define SIZE_OF(x) sizeof(x)
#define SIZE_OF_FOO SIZE_OF(struct foo)
#define VAR1 VAR
#define VAR var
int var = 5;
#define ADDR &var
#define CALL fn()
int fn(void) {
return ++var;
}
// issue 20129
int issue20129 = 0;
typedef void issue20129Void;
issue20129Void issue20129Foo() {
issue20129 = 1;
}
typedef issue20129Void issue20129Void2;
issue20129Void2 issue20129Bar() {
issue20129 = 2;
}
// issue 20369
#define XUINT64_MAX 18446744073709551615ULL
// issue 21668
// Fail to guess the kind of the constant "x".
// No runtime test; just make sure it compiles.
const int x21668 = 42;
// issue 21708
#define CAST_TO_INT64 (int64_t)(-1)
// issue 21809
// Compile C `typedef` to go type aliases.
typedef long MySigned_t;
// tests alias-to-alias
typedef MySigned_t MySigned2_t;
long takes_long(long x) { return x * x; }
MySigned_t takes_typedef(MySigned_t x) { return x * x; }
// issue 22906
// It's going to be hard to include a whole real JVM to test this.
// So we'll simulate a really easy JVM using just the parts we need.
// This is the relevant part of jni.h.
struct _jobject;
typedef struct _jobject *jobject;
typedef jobject jclass;
typedef jobject jthrowable;
typedef jobject jstring;
typedef jobject jarray;
typedef jarray jbooleanArray;
typedef jarray jbyteArray;
typedef jarray jcharArray;
typedef jarray jshortArray;
typedef jarray jintArray;
typedef jarray jlongArray;
typedef jarray jfloatArray;
typedef jarray jdoubleArray;
typedef jarray jobjectArray;
typedef jobject jweak;
// Note: jvalue is already a non-pointer type due to it being a C union.
// issue 22958
typedef struct {
unsigned long long f8 : 8;
unsigned long long f16 : 16;
unsigned long long f24 : 24;
unsigned long long f32 : 32;
unsigned long long f40 : 40;
unsigned long long f48 : 48;
unsigned long long f56 : 56;
unsigned long long f64 : 64;
} issue22958Type;
// issue 23356
int a(void) { return 5; };
int r(void) { return 3; };
// issue 23720
typedef int *issue23720A;
typedef const int *issue23720B;
void issue23720F(issue23720B a) {}
// issue 24206
#if defined(__linux__) && defined(__x86_64__)
#include <sys/mman.h>
// Returns string with null byte at the last valid address
char* dangerousString1() {
int pageSize = 4096;
char *data = mmap(0, 2 * pageSize, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, 0, 0);
mprotect(data + pageSize,pageSize,PROT_NONE);
int start = pageSize - 123 - 1; // last 123 bytes of first page + 1 null byte
int i = start;
for (; i < pageSize; i++) {
data[i] = 'x';
}
data[pageSize -1 ] = 0;
return data+start;
}
char* dangerousString2() {
int pageSize = 4096;
char *data = mmap(0, 3 * pageSize, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, 0, 0);
mprotect(data + 2 * pageSize,pageSize,PROT_NONE);
int start = pageSize - 123 - 1; // last 123 bytes of first page + 1 null byte
int i = start;
for (; i < 2 * pageSize; i++) {
data[i] = 'x';
}
data[2*pageSize -1 ] = 0;
return data+start;
}
#else
char *dangerousString1() { return NULL; }
char *dangerousString2() { return NULL; }
#endif
// issue 26066
const unsigned long long int issue26066 = (const unsigned long long) -1;
// issue 26517
// Introduce two pointer types which are distinct, but have the same
// base type. Make sure that both of those pointer types get resolved
// correctly. Before the fix for 26517 if one of these pointer types
// was resolved before the other one was processed, the second one
// would never be resolved.
// Before this issue was fixed this test failed on Windows,
// where va_list expands to a named char* type.
typedef va_list TypeOne;
typedef char *TypeTwo;
// issue 28540
static void twoargs1(void *p, int n) {}
static void *twoargs2() { return 0; }
static int twoargs3(void * p) { return 0; }
// issue 28545
// Failed to add type conversion for negative constant.
static void issue28545F(char **p, int n, complex double a) {}
// issue 28772 part 1 - part 2 in testx.go
// Failed to add type conversion for Go constant set to C constant.
// No runtime test; just make sure it compiles.
#define issue28772Constant 1
// issue 28896
// cgo was incorrectly adding padding after a packed struct.
typedef struct {
void *f1;
uint32_t f2;
} __attribute__((__packed__)) innerPacked;
typedef struct {
innerPacked g1;
uint64_t g2;
} outerPacked;
typedef struct {
void *f1;
uint32_t f2;
} innerUnpacked;
typedef struct {
innerUnpacked g1;
uint64_t g2;
} outerUnpacked;
size_t offset(int x) {
switch (x) {
case 0:
return offsetof(innerPacked, f2);
case 1:
return offsetof(outerPacked, g2);
case 2:
return offsetof(innerUnpacked, f2);
case 3:
return offsetof(outerUnpacked, g2);
default:
abort();
}
}
// issue 29748
typedef struct { char **p; } S29748;
static int f29748(S29748 *p) { return 0; }
// issue 29781
// Error with newline inserted into constant expression.
// Compilation test only, nothing to run.
static void issue29781F(char **p, int n) {}
#define ISSUE29781C 0
// issue 31093
static uint16_t issue31093F(uint16_t v) { return v; }
// issue 32579
typedef struct S32579 { unsigned char data[1]; } S32579;
// issue 37033, cgo.Handle
extern void GoFunc37033(uintptr_t handle);
void cFunc37033(uintptr_t handle) { GoFunc37033(handle); }
// issue 38649
// Test that #define'd type aliases work.
#define netbsd_gid unsigned int
// issue 40494
// Inconsistent handling of tagged enum and union types.
enum Enum40494 { X_40494 };
union Union40494 { int x; };
void issue40494(enum Enum40494 e, union Union40494* up) {}
// Issue 45451, bad handling of go:notinheap types.
typedef struct issue45451Undefined issue45451;
// Issue 49633, example of cgo.Handle with void*.
extern void GoFunc49633(void*);
void cfunc49633(void *context) { GoFunc49633(context); }
*/
import "C"
import (
"context"
"fmt"
"math"
"math/rand"
"os"
"os/signal"
"reflect"
"runtime"
"runtime/cgo"
"sync"
"syscall"
"testing"
"time"
"unsafe"
)
// alignment
func testAlign(t *testing.T) {
var evt C.SDL_KeyboardEvent
C.makeEvent(&evt)
if C.same(&evt, evt.typ, evt.which, evt.state, evt.keysym.scancode, evt.keysym.sym, evt.keysym.mod, evt.keysym.unicode) == 0 {
t.Error("*** bad alignment")
C.cTest(&evt)
t.Errorf("Go: %#x %#x %#x %#x %#x %#x %#x\n",
evt.typ, evt.which, evt.state, evt.keysym.scancode,
evt.keysym.sym, evt.keysym.mod, evt.keysym.unicode)
t.Error(evt)
}
}
// api
const greeting = "hello, world"
type testPair struct {
Name string
Got, Want interface{}
}
var testPairs = []testPair{
{"GoString", C.GoString(C.greeting), greeting},
{"GoStringN", C.GoStringN(C.greeting, 5), greeting[:5]},
{"GoBytes", C.GoBytes(unsafe.Pointer(C.greeting), 5), []byte(greeting[:5])},
}
func testHelpers(t *testing.T) {
for _, pair := range testPairs {
if !reflect.DeepEqual(pair.Got, pair.Want) {
t.Errorf("%s: got %#v, want %#v", pair.Name, pair.Got, pair.Want)
}
}
}
// basic test cases
const EINVAL = C.EINVAL /* test #define */
var KILO = C.KILO
func uuidgen() {
var uuid C.cgo_uuid_t
C.uuid_generate(&uuid[0])
}
func Strtol(s string, base int) (int, error) {
p := C.CString(s)
n, err := C.strtol(p, nil, C.int(base))
C.free(unsafe.Pointer(p))