Home | History | Annotate | Download | only in test
      1 // Copyright 2010 The Go Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style
      3 // license that can be found in the LICENSE file.
      4 
      5 // This C program generates the file cmplxdivide1.go. It uses the
      6 // output of the operations by C99 as the reference to check
      7 // the implementation of complex numbers in Go.
      8 // The generated file, cmplxdivide1.go, is compiled along
      9 // with the driver cmplxdivide.go (the names are confusing
     10 // and unimaginative) to run the actual test. This is done by
     11 // the usual test runner.
     12 //
     13 // The file cmplxdivide1.go is checked in to the repository, but
     14 // if it needs to be regenerated, compile and run this C program
     15 // like this:
     16 //	gcc '-std=c99' cmplxdivide.c && a.out >cmplxdivide1.go
     17 
     18 #include <complex.h>
     19 #include <math.h>
     20 #include <stdio.h>
     21 #include <string.h>
     22 
     23 #define nelem(x) (sizeof(x)/sizeof((x)[0]))
     24 
     25 double f[] = {
     26 	0.0,
     27 	-0.0,
     28 	1.0,
     29 	-1.0,
     30 	2.0,
     31 	NAN,
     32 	INFINITY,
     33 	-INFINITY,
     34 };
     35 
     36 char* fmt(double g) {
     37 	static char buf[10][30];
     38 	static int n;
     39 	char *p;
     40 
     41 	p = buf[n++];
     42 	if(n == 10) {
     43 		n = 0;
     44 	}
     45 
     46 	sprintf(p, "%g", g);
     47 
     48 	if(strcmp(p, "0") == 0) {
     49 		strcpy(p, "zero");
     50 		return p;
     51 	}
     52 
     53 	if(strcmp(p, "-0") == 0) {
     54 		strcpy(p, "-zero");
     55 		return p;
     56 	}
     57 
     58 	return p;
     59 }
     60 
     61 int main(void) {
     62 	int i, j, k, l;
     63 	double complex n, d, q;
     64 
     65 	printf("// skip\n");
     66 	printf("// # generated by cmplxdivide.c\n");
     67 	printf("\n");
     68 	printf("package main\n");
     69 	printf("\n");
     70 	printf("import \"math\"\n");
     71 	printf("\n");
     72 	printf("var (\n");
     73 	printf("\tnan     = math.NaN()\n");
     74 	printf("\tinf     = math.Inf(1)\n");
     75 	printf("\tzero    = 0.0\n");
     76 	printf(")\n");
     77 	printf("\n");
     78 	printf("var tests = []struct {\n");
     79 	printf("\tf, g complex128\n");
     80 	printf("\tout  complex128\n");
     81 	printf("}{\n");
     82 
     83 	for(i=0; i<nelem(f); i++)
     84 	for(j=0; j<nelem(f); j++)
     85 	for(k=0; k<nelem(f); k++)
     86 	for(l=0; l<nelem(f); l++) {
     87 		n = f[i] + f[j]*I;
     88 		d = f[k] + f[l]*I;
     89 		q = n/d;
     90 
     91 		printf("\t{complex(%s, %s), complex(%s, %s), complex(%s, %s)},\n",
     92 			fmt(creal(n)), fmt(cimag(n)),
     93 			fmt(creal(d)), fmt(cimag(d)),
     94 			fmt(creal(q)), fmt(cimag(q)));
     95 	}
     96 	printf("}\n");
     97 	return 0;
     98 }
     99