LEFT | RIGHT |
(no file at all) | |
1 // Copyright 2010 The Go Authors. All rights reserved. | 1 // Copyright 2010 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // This file contains test cases for cgo. | 5 // Basic test cases for cgo. |
6 | 6 |
7 package stdio | 7 package cgotest |
8 | 8 |
9 /* | 9 /* |
10 #include <stdio.h> | 10 #include <stdio.h> |
11 #include <stdlib.h> | 11 #include <stdlib.h> |
12 #include <sys/stat.h> | 12 #include <sys/stat.h> |
13 #include <errno.h> | 13 #include <errno.h> |
14 | 14 |
15 #define SHIFT(x, y) ((x)<<(y)) | 15 #define SHIFT(x, y) ((x)<<(y)) |
16 #define KILO SHIFT(1, 10) | 16 #define KILO SHIFT(1, 10) |
17 | 17 |
(...skipping 27 matching lines...) Expand all Loading... |
45 } element; | 45 } element; |
46 }; | 46 }; |
47 | 47 |
48 struct ibv_context { | 48 struct ibv_context { |
49 xxpthread_mutex_t mutex; | 49 xxpthread_mutex_t mutex; |
50 }; | 50 }; |
51 */ | 51 */ |
52 import "C" | 52 import "C" |
53 import ( | 53 import ( |
54 "os" | 54 "os" |
| 55 "testing" |
55 "unsafe" | 56 "unsafe" |
56 ) | 57 ) |
57 | 58 |
58 const EINVAL = C.EINVAL /* test #define */ | 59 const EINVAL = C.EINVAL /* test #define */ |
59 | 60 |
60 var KILO = C.KILO | 61 var KILO = C.KILO |
61 | 62 |
62 func uuidgen() { | 63 func uuidgen() { |
63 var uuid C.uuid_t | 64 var uuid C.uuid_t |
64 C.uuid_generate(&uuid[0]) | 65 C.uuid_generate(&uuid[0]) |
(...skipping 17 matching lines...) Expand all Loading... |
82 return int(n), err | 83 return int(n), err |
83 } | 84 } |
84 | 85 |
85 func Atol(s string) int { | 86 func Atol(s string) int { |
86 p := C.CString(s) | 87 p := C.CString(s) |
87 n := C.atol(p) | 88 n := C.atol(p) |
88 C.free(unsafe.Pointer(p)) | 89 C.free(unsafe.Pointer(p)) |
89 return int(n) | 90 return int(n) |
90 } | 91 } |
91 | 92 |
92 func TestConst() { | 93 func TestConst(t *testing.T) { |
93 C.myConstFunc(nil, 0, nil) | 94 C.myConstFunc(nil, 0, nil) |
94 } | 95 } |
95 | 96 |
96 func TestEnum() { | 97 func TestEnum(t *testing.T) { |
97 if C.Enum1 != 1 || C.Enum2 != 2 { | 98 if C.Enum1 != 1 || C.Enum2 != 2 { |
98 » » println("bad enum", C.Enum1, C.Enum2) | 99 » » t.Error("bad enum", C.Enum1, C.Enum2) |
99 } | 100 } |
100 } | 101 } |
101 | 102 |
102 func TestAtol() { | 103 func TestAtol(t *testing.T) { |
103 l := Atol("123") | 104 l := Atol("123") |
104 if l != 123 { | 105 if l != 123 { |
105 » » println("Atol 123: ", l) | 106 » » t.Error("Atol 123: ", l) |
106 » » panic("bad atol") | |
107 } | 107 } |
108 } | 108 } |
109 | 109 |
110 func TestErrno() { | 110 func TestErrno(t *testing.T) { |
111 n, err := Strtol("asdf", 123) | 111 n, err := Strtol("asdf", 123) |
112 if n != 0 || err != os.EINVAL { | 112 if n != 0 || err != os.EINVAL { |
113 » » println("Strtol: ", n, err) | 113 » » t.Error("Strtol: ", n, err) |
114 » » panic("bad strtol") | |
115 } | 114 } |
116 } | 115 } |
117 | 116 |
118 func TestMultipleAssign() { | 117 func TestMultipleAssign(t *testing.T) { |
119 » p := C.CString("123") | 118 » p := C.CString("234") |
120 n, m := C.strtol(p, nil, 345), C.strtol(p, nil, 10) | 119 n, m := C.strtol(p, nil, 345), C.strtol(p, nil, 10) |
121 if n != 0 || m != 234 { | 120 if n != 0 || m != 234 { |
122 » » println("Strtol x2: ", n, m) | 121 » » t.Fatal("Strtol x2: ", n, m) |
123 » » panic("bad strtol x2") | |
124 } | 122 } |
125 C.free(unsafe.Pointer(p)) | 123 C.free(unsafe.Pointer(p)) |
126 } | 124 } |
127 | 125 |
128 var ( | 126 var ( |
129 uint = (C.uint)(0) | 127 uint = (C.uint)(0) |
130 ulong C.ulong | 128 ulong C.ulong |
131 char C.char | 129 char C.char |
132 ) | 130 ) |
133 | 131 |
134 type Context struct { | 132 type Context struct { |
135 ctx *C.struct_ibv_context | 133 ctx *C.struct_ibv_context |
136 } | 134 } |
137 | |
138 func Test() { | |
139 TestAlign() | |
140 TestAtol() | |
141 TestEnum() | |
142 TestErrno() | |
143 TestConst() | |
144 } | |
LEFT | RIGHT |