blob: 89a2868b75bdb249b905f4243cd5c1f8485569a8 [file] [log] [blame]
Emmanuel Odeke53fd5222016-04-10 14:32:26 -07001// Copyright 2010 The Go Authors. All rights reserved.
Russ Cox21ff75b2010-06-18 15:46:00 -07002// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
Rob Pike3b63b692015-01-15 10:43:04 +11005// 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
Russ Cox21ff75b2010-06-18 15:46:00 -070017
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
25double f[] = {
Martin Möhrmann16200c72017-02-25 23:50:56 +010026 0.0,
27 -0.0,
28 1.0,
29 -1.0,
30 2.0,
Russ Cox21ff75b2010-06-18 15:46:00 -070031 NAN,
32 INFINITY,
33 -INFINITY,
34};
35
Martin Möhrmann16200c72017-02-25 23:50:56 +010036char* fmt(double g) {
Russ Cox21ff75b2010-06-18 15:46:00 -070037 static char buf[10][30];
38 static int n;
39 char *p;
Martin Möhrmann16200c72017-02-25 23:50:56 +010040
Russ Cox21ff75b2010-06-18 15:46:00 -070041 p = buf[n++];
Martin Möhrmann16200c72017-02-25 23:50:56 +010042 if(n == 10) {
Russ Cox21ff75b2010-06-18 15:46:00 -070043 n = 0;
Martin Möhrmann16200c72017-02-25 23:50:56 +010044 }
45
Russ Cox21ff75b2010-06-18 15:46:00 -070046 sprintf(p, "%g", g);
Martin Möhrmann16200c72017-02-25 23:50:56 +010047
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
Russ Cox21ff75b2010-06-18 15:46:00 -070058 return p;
Russ Cox47c85ec2010-06-30 23:34:27 -070059}
60
Martin Möhrmann16200c72017-02-25 23:50:56 +010061int main(void) {
Russ Cox21ff75b2010-06-18 15:46:00 -070062 int i, j, k, l;
63 double complex n, d, q;
Martin Möhrmann16200c72017-02-25 23:50:56 +010064
Shenghou Mae2662832012-03-22 02:14:44 +080065 printf("// skip\n");
Russ Cox21ff75b2010-06-18 15:46:00 -070066 printf("// # generated by cmplxdivide.c\n");
67 printf("\n");
68 printf("package main\n");
Martin Möhrmann16200c72017-02-25 23:50:56 +010069 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
Russ Cox21ff75b2010-06-18 15:46:00 -070083 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;
Russ Cox47c85ec2010-06-30 23:34:27 -070090
Martin Möhrmann16200c72017-02-25 23:50:56 +010091 printf("\t{complex(%s, %s), complex(%s, %s), complex(%s, %s)},\n",
Russ Cox47c85ec2010-06-30 23:34:27 -070092 fmt(creal(n)), fmt(cimag(n)),
93 fmt(creal(d)), fmt(cimag(d)),
94 fmt(creal(q)), fmt(cimag(q)));
Russ Cox21ff75b2010-06-18 15:46:00 -070095 }
96 printf("}\n");
97 return 0;
98}