• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <stdio.h>
2 #include <stdlib.h>
3 
4 double drand48(void);
5 
6 static int fails = 0;
7 
8 static int
double_eq(double a,double b)9 double_eq(double a, double b)
10 {
11     /* Compare two double values, and return 1 if they are "close" enough */
12     double diff = a -b;
13     if (diff < 0) diff = -diff;
14     if (a < 0) {
15         if (b >= 0)
16             return 0;
17         a = -a;
18         b = -b;
19     } else if (b < 0) {
20         return 0;
21     }
22     if (a >= b)
23         a = b;
24 
25     return diff < a*1e-8;
26 }
27 
28 #define EXPECT_LONG(value,expected) \
29     do { \
30         long _val = (value); \
31         long _expected = (expected); \
32         printf( "%s: ", #value); \
33         if (_val != _expected) { \
34             printf("KO: %ld (%ld expected)\n", _val, _expected); \
35             fails += 1; \
36         } else { \
37             printf("%ld (ok)\n", _expected); \
38         } \
39     } while (0)
40 
41 #define EXPECT_DOUBLE(value,expected) \
42     do { \
43         double _val = (value); \
44         double _expected = (expected); \
45         printf( "%s: ", #value); \
46         if (!double_eq(_val,_expected)) { \
47             printf("KO: %.12g (%.12g expected)\n", _val, _expected); \
48             fails += 1; \
49         } else { \
50             printf("%.12g (ok)\n", _expected); \
51         } \
52     } while (0)
53 
54 int
main(void)55 main(void)
56 {
57     long int l = -345678L;
58     float  f = 123.456e14;
59     double d = -87.65432e45;
60 
61     // Verify display of hard-coded float and double values.
62     // This is done to confirm the correct printf format specifiers
63     // are being used.
64     puts("Hard-coded values");
65     printf("  l: %li\n", l);
66     printf("  f: %g\n", (double) f);
67     printf("  d: %g\n", d);
68 
69     // lrand48
70     puts("lrand48");
71     puts("  srand48(100)");
72     srand48(100);
73     EXPECT_LONG(lrand48(),539144888);
74     EXPECT_LONG(lrand48(),448713282);
75     EXPECT_LONG(lrand48(),2020627300);
76 
77     // Try again, with same seed.  Should get the same values
78     puts("  srand48(100)");
79     srand48(100);
80     EXPECT_LONG(lrand48(),539144888);
81     EXPECT_LONG(lrand48(),448713282);
82     EXPECT_LONG(lrand48(),2020627300);
83 
84     // Try again, but with a different seed
85     puts("  srand48(101)");
86     srand48(101);
87     EXPECT_LONG(lrand48(),261694958);
88     EXPECT_LONG(lrand48(),1961809783);
89     EXPECT_LONG(lrand48(),1458943423);
90 
91     // drand48
92     puts("drand48");
93     puts("  srand48(100)");
94     srand48(100);
95     EXPECT_DOUBLE(drand48(),0.251058902665);
96     EXPECT_DOUBLE(drand48(),0.208948404851);
97     EXPECT_DOUBLE(drand48(),0.940927909958);
98 
99     // Try again, with same seed.  Should get the same values
100     puts("  srand48(100)");
101     srand48(100);
102     EXPECT_DOUBLE(drand48(),0.251058902665);
103     EXPECT_DOUBLE(drand48(),0.208948404851);
104     EXPECT_DOUBLE(drand48(),0.940927909958);
105 
106     // Try again, but with a different seed
107     puts("  srand48(101)");
108     srand48(101);
109     EXPECT_DOUBLE(drand48(),0.121861211331);
110     EXPECT_DOUBLE(drand48(),0.913538869095);
111     EXPECT_DOUBLE(drand48(),0.679373472502);
112 
113     return (fails > 0) ? 1 : 0;
114 }
115