github.com/gotranspile/cxgo@v0.3.7/libs_test.go (about) 1 package cxgo 2 3 import "testing" 4 5 var casesTranslateLibs = []parseCase{ 6 { 7 name: "panic", 8 src: ` 9 #include <stdlib.h> 10 11 void foo() { 12 abort(); 13 __builtin_abort(); 14 __builtin_trap(); 15 __builtin_unreachable(); 16 } 17 `, 18 exp: ` 19 func foo() { 20 panic("abort") 21 panic("abort") 22 panic("trap") 23 panic("unreachable") 24 } 25 `, 26 }, 27 { 28 name: "assert", 29 src: ` 30 #include <assert.h> 31 32 void foo(int a) { 33 assert(a); 34 assert(a != 5); 35 assert(0); 36 assert(!"fail"); 37 } 38 `, 39 exp: ` 40 func foo(a int32) { 41 if a == 0 { 42 panic("assert failed") 43 } 44 if a == 5 { 45 panic("assert failed") 46 } 47 panic(0) 48 panic("fail") 49 } 50 `, 51 }, 52 { 53 name: "varargs", 54 src: ` 55 #include <stdarg.h> 56 57 void foo(int a, ...) { 58 va_list va; 59 va_start(va, a); 60 int b = va_arg(va, int); 61 } 62 `, 63 exp: ` 64 func foo(a int32, _rest ...interface{}) { 65 var va libc.ArgList 66 va.Start(a, _rest) 67 var b int32 = va.Arg().(int32) 68 _ = b 69 } 70 `, 71 }, 72 { 73 name: "inet", 74 src: ` 75 #include <arpa/inet.h> 76 77 void foo() { 78 in_addr_t a = inet_addr("1.2.3.4"); 79 } 80 `, 81 exp: ` 82 func foo() { 83 var a cnet.Addr = cnet.ParseAddr("1.2.3.4") 84 _ = a 85 } 86 `, 87 }, 88 { 89 name: "sys socket", 90 src: ` 91 #include <sys/socket.h> 92 93 void foo() { 94 in_addr_t a = inet_addr("1.2.3.4"); 95 } 96 `, 97 exp: ` 98 func foo() { 99 var a cnet.Addr = cnet.ParseAddr("1.2.3.4") 100 _ = a 101 } 102 `, 103 }, 104 { 105 name: "math", 106 src: ` 107 #include <math.h> 108 109 void foo(float x, double y) { 110 x = M_PI; y = M_PI; 111 x = modff(x, &x); y = modf(y, &y); 112 x = sinf(x); y = sin(y); 113 x = coshf(x); y = cosh(y); 114 x = atanf(x); y = atan(y); 115 x = roundf(x); y = round(y); 116 x = fabsf(x); y = fabs(y); 117 x = powf(x, x); y = pow(y, y); 118 y = fmod(y, y); 119 } 120 `, 121 exp: ` 122 func foo(x float32, y float64) { 123 x = float32(math.Pi) 124 y = math.Pi 125 x = cmath.Modff(x, &x) 126 y = cmath.Modf(y, &y) 127 x = math32.Sin(x) 128 y = math.Sin(y) 129 x = math32.Cosh(x) 130 y = math.Cosh(y) 131 x = math32.Atan(x) 132 y = math.Atan(y) 133 x = float32(math.Round(float64(x))) 134 y = math.Round(y) 135 x = math32.Abs(x) 136 y = math.Abs(y) 137 x = math32.Pow(x, x) 138 y = math.Pow(y, y) 139 y = math.Mod(y, y) 140 } 141 `, 142 }, 143 { 144 name: "abs", 145 src: ` 146 #include <stdlib.h> 147 #include <math.h> 148 149 void foo(int abs) {} 150 `, 151 exp: ` 152 func foo(abs int32) { 153 } 154 `, 155 }, 156 { 157 name: "main no args", 158 src: ` 159 #include <stdlib.h> 160 161 void main() { 162 exit(0); 163 } 164 `, 165 exp: ` 166 func main() { 167 os.Exit(0) 168 } 169 `, 170 }, 171 { 172 name: "main with args", 173 src: ` 174 int main(int argc, char **argv) { 175 if (argc == 1) { 176 return argv != 0; 177 } 178 return 1; 179 } 180 `, 181 exp: ` 182 func main() { 183 var ( 184 argc int32 = int32(len(os.Args)) 185 argv **byte = libc.CStringSlice(os.Args) 186 ) 187 if argc == 1 { 188 os.Exit(int(libc.BoolToInt(argv != nil))) 189 } 190 os.Exit(1) 191 } 192 `, 193 }, 194 { 195 name: "undef malloc", 196 src: ` 197 #include <stdlib.h> 198 199 #undef malloc 200 void foo() { 201 void* p = malloc(10); 202 } 203 `, 204 exp: ` 205 func foo() { 206 var p unsafe.Pointer = libc.Malloc(10) 207 _ = p 208 } 209 `, 210 }, 211 } 212 213 var casesRunLibs = []struct { 214 name string 215 src string 216 }{ 217 { 218 name: "strtok", 219 src: ` 220 #include <string.h> 221 #include <stdio.h> 222 223 void printStr(const char* name, const char* s) { 224 printf("%s, %d: '%s'\n", name, strlen(s), s); 225 } 226 227 int main() { 228 const char* s = "_ a some __string here"; 229 char* buf = malloc(strlen(s)+1); 230 strcpy(buf, s); 231 printStr("buf 0", buf); 232 233 char* tok = strtok(buf, " _"); 234 printStr("tok 1", tok); 235 printStr("buf 1", buf); 236 printf("diff 1: %d\n", tok-buf); 237 for (int i = 0; i < 5; i++) { 238 tok = strtok(0, " _"); 239 if (!tok) break; 240 printStr("tok N", tok); 241 printf("diff N: %d\n", tok-buf); 242 } 243 printStr("buf end", buf); 244 free(buf); 245 return 0; 246 } 247 `, 248 }, 249 { 250 name: "char", 251 src: ` 252 #include <stdio.h> 253 254 int main() { 255 char a = 0; 256 char b = a-1; 257 printf("%d\n", (int)b); 258 return 0; 259 } 260 `, 261 }, 262 { 263 name: "uchar", 264 src: ` 265 #include <stdio.h> 266 267 int main() { 268 unsigned char a = 0; 269 unsigned char b = a-1; 270 printf("%d\n", (int)b); 271 return 0; 272 } 273 `, 274 }, 275 { 276 name: "schar", 277 src: ` 278 #include <stdio.h> 279 280 int main() { 281 signed char a = 0; 282 signed char b = a-1; 283 printf("%d\n", (int)b); 284 return 0; 285 } 286 `, 287 }, 288 { 289 name: "char mult overflow", 290 src: ` 291 #include <stdio.h> 292 293 int main() { 294 char a = 100; 295 char b = 200; 296 char x = a * b; 297 int y = a * b; 298 printf("%d, %d\n", (int)x, y); 299 } 300 `, 301 }, 302 { 303 name: "int mult overflow", 304 src: ` 305 #include <stdio.h> 306 307 int main() { 308 int a = 0xf000000; 309 int b = 0x1000000; 310 int x = a * b; 311 long long y = a * b; 312 long long z = (long long)a * b; 313 printf("%d, %lld, %lld\n", x, y, z); 314 } 315 `, 316 }, 317 { 318 name: "func name", 319 src: ` 320 #include <stdio.h> 321 322 void foo() { 323 printf("%s, %s, %s\n", __func__, __FUNCTION__, __PRETTY_FUNCTION__); 324 } 325 326 int main() { 327 foo(); 328 } 329 `, 330 }, 331 } 332 333 func TestTranslateLibs(t *testing.T) { 334 runTestTranslate(t, casesTranslateLibs) 335 } 336 337 func TestRunLibs(t *testing.T) { 338 for _, c := range casesRunLibs { 339 c := c 340 t.Run(c.name, func(t *testing.T) { 341 testTranspileOut(t, c.src) 342 }) 343 } 344 }