Emmanuel Odeke | 53fd522 | 2016-04-10 14:32:26 -0700 | [diff] [blame] | 1 | // Copyright 2010 The Go Authors. All rights reserved. |
Russ Cox | 21ff75b | 2010-06-18 15:46:00 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
Rob Pike | 3b63b69 | 2015-01-15 10:43:04 +1100 | [diff] [blame] | 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 |
Russ Cox | 21ff75b | 2010-06-18 15:46:00 -0700 | [diff] [blame] | 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[] = { |
Martin Möhrmann | 16200c7 | 2017-02-25 23:50:56 +0100 | [diff] [blame] | 26 | 0.0, |
| 27 | -0.0, |
| 28 | 1.0, |
| 29 | -1.0, |
| 30 | 2.0, |
Russ Cox | 21ff75b | 2010-06-18 15:46:00 -0700 | [diff] [blame] | 31 | NAN, |
| 32 | INFINITY, |
| 33 | -INFINITY, |
| 34 | }; |
| 35 | |
Martin Möhrmann | 16200c7 | 2017-02-25 23:50:56 +0100 | [diff] [blame] | 36 | char* fmt(double g) { |
Russ Cox | 21ff75b | 2010-06-18 15:46:00 -0700 | [diff] [blame] | 37 | static char buf[10][30]; |
| 38 | static int n; |
| 39 | char *p; |
Martin Möhrmann | 16200c7 | 2017-02-25 23:50:56 +0100 | [diff] [blame] | 40 | |
Russ Cox | 21ff75b | 2010-06-18 15:46:00 -0700 | [diff] [blame] | 41 | p = buf[n++]; |
Martin Möhrmann | 16200c7 | 2017-02-25 23:50:56 +0100 | [diff] [blame] | 42 | if(n == 10) { |
Russ Cox | 21ff75b | 2010-06-18 15:46:00 -0700 | [diff] [blame] | 43 | n = 0; |
Martin Möhrmann | 16200c7 | 2017-02-25 23:50:56 +0100 | [diff] [blame] | 44 | } |
| 45 | |
Russ Cox | 21ff75b | 2010-06-18 15:46:00 -0700 | [diff] [blame] | 46 | sprintf(p, "%g", g); |
Martin Möhrmann | 16200c7 | 2017-02-25 23:50:56 +0100 | [diff] [blame] | 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 | |
Russ Cox | 21ff75b | 2010-06-18 15:46:00 -0700 | [diff] [blame] | 58 | return p; |
Russ Cox | 47c85ec | 2010-06-30 23:34:27 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Martin Möhrmann | 16200c7 | 2017-02-25 23:50:56 +0100 | [diff] [blame] | 61 | int main(void) { |
Russ Cox | 21ff75b | 2010-06-18 15:46:00 -0700 | [diff] [blame] | 62 | int i, j, k, l; |
| 63 | double complex n, d, q; |
Martin Möhrmann | 16200c7 | 2017-02-25 23:50:56 +0100 | [diff] [blame] | 64 | |
Shenghou Ma | e266283 | 2012-03-22 02:14:44 +0800 | [diff] [blame] | 65 | printf("// skip\n"); |
Russ Cox | 21ff75b | 2010-06-18 15:46:00 -0700 | [diff] [blame] | 66 | printf("// # generated by cmplxdivide.c\n"); |
| 67 | printf("\n"); |
| 68 | printf("package main\n"); |
Martin Möhrmann | 16200c7 | 2017-02-25 23:50:56 +0100 | [diff] [blame] | 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 | |
Russ Cox | 21ff75b | 2010-06-18 15:46:00 -0700 | [diff] [blame] | 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; |
Russ Cox | 47c85ec | 2010-06-30 23:34:27 -0700 | [diff] [blame] | 90 | |
Martin Möhrmann | 16200c7 | 2017-02-25 23:50:56 +0100 | [diff] [blame] | 91 | printf("\t{complex(%s, %s), complex(%s, %s), complex(%s, %s)},\n", |
Russ Cox | 47c85ec | 2010-06-30 23:34:27 -0700 | [diff] [blame] | 92 | fmt(creal(n)), fmt(cimag(n)), |
| 93 | fmt(creal(d)), fmt(cimag(d)), |
| 94 | fmt(creal(q)), fmt(cimag(q))); |
Russ Cox | 21ff75b | 2010-06-18 15:46:00 -0700 | [diff] [blame] | 95 | } |
| 96 | printf("}\n"); |
| 97 | return 0; |
| 98 | } |