summaryrefslogtreecommitdiff
path: root/misc/cgo/stdio/test.go
blob: 8f21603ca32e0f3838105a847e3158ca297b3729 (plain)
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
// 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.

// This file contains test cases for cgo.

package stdio

/*
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <errno.h>

#define SHIFT(x, y)  ((x)<<(y))
#define KILO SHIFT(1, 10)

enum E {
	Enum1 = 1,
	Enum2 = 2,
};

typedef unsigned char uuid_t[20];

void uuid_generate(uuid_t x) {
	x[0] = 0;
}

struct S {
	int x;
};

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; }

// issue 1222
typedef union {
	long align;
} xxpthread_mutex_t;

struct ibv_async_event {
	union {
		int x;
	} element;
};

struct ibv_context {
	xxpthread_mutex_t mutex;
};
*/
import "C"
import (
	"os"
	"unsafe"
)

const EINVAL = C.EINVAL /* test #define */

var KILO = C.KILO

func uuidgen() {
	var uuid C.uuid_t
	C.uuid_generate(&uuid[0])
}

func Size(name string) (int64, os.Error) {
	var st C.struct_stat
	p := C.CString(name)
	_, err := C.stat(p, &st)
	C.free(unsafe.Pointer(p))
	if err != nil {
		return 0, err
	}
	return int64(C.ulong(st.st_size)), nil
}

func Strtol(s string, base int) (int, os.Error) {
	p := C.CString(s)
	n, err := C.strtol(p, nil, C.int(base))
	C.free(unsafe.Pointer(p))
	return int(n), err
}

func Atol(s string) int {
	p := C.CString(s)
	n := C.atol(p)
	C.free(unsafe.Pointer(p))
	return int(n)
}

func TestConst() {
	C.myConstFunc(nil, 0, nil)
}

func TestEnum() {
	if C.Enum1 != 1 || C.Enum2 != 2 {
		println("bad enum", C.Enum1, C.Enum2)
	}
}

func TestAtol() {
	l := Atol("123")
	if l != 123 {
		println("Atol 123: ", l)
		panic("bad atol")
	}
}

func TestErrno() {
	n, err := Strtol("asdf", 123)
	if n != 0 || err != os.EINVAL {
		println("Strtol: ", n, err)
		panic("bad strtol")
	}
}

func TestMultipleAssign() {
	p := C.CString("123")
	n, m := C.strtol(p, nil, 345), C.strtol(p, nil, 10)
	if n != 0 || m != 234 {
		println("Strtol x2: ", n, m)
		panic("bad strtol x2")
	}
	C.free(unsafe.Pointer(p))
}

var (
	uint  = (C.uint)(0)
	ulong C.ulong
	char  C.char
)

type Context struct {
	ctx *C.struct_ibv_context
}

func Test() {
	TestAlign()
	TestAtol()
	TestEnum()
	TestErrno()
	TestConst()
}