1+ /**
2+ * @license Apache-2.0
3+ *
4+ * Copyright (c) 2024 The Stdlib Authors.
5+ *
6+ * Licensed under the Apache License, Version 2.0 (the "License");
7+ * you may not use this file except in compliance with the License.
8+ * You may obtain a copy of the License at
9+ *
10+ * http://www.apache.org/licenses/LICENSE-2.0
11+ *
12+ * Unless required by applicable law or agreed to in writing, software
13+ * distributed under the License is distributed on an "AS IS" BASIS,
14+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+ * See the License for the specific language governing permissions and
16+ * limitations under the License.
17+ */
18+
19+ /**
20+ * Generate C test fixtures for tinymt32.
21+ *
22+ * ## Notes
23+ *
24+ * - Run this script from the directory in which fixtures should be written.
25+ *
26+ */
27+
28+ #include <stdlib.h>
29+ #include <stdio.h>
30+ #include "tinymt32.h"
31+
32+ // Helper functions remain the same as MT19937...
33+ // ... (linspace, write functions, etc.)
34+
35+ /**
36+ * Writes an array of doubles to a file as a series of comma-separated values.
37+ *
38+ * @param f file to write to
39+ * @param x array of doubles
40+ * @param len array length
41+ */
42+ void write_array_f64 (FILE * f , const double * x , const unsigned int len ) {
43+ unsigned int i ;
44+
45+ for (i = 0 ; i < len ; i ++ ) {
46+ fprintf (f , "%.17g" , x [i ]);
47+ if (i < len - 1 ) {
48+ fprintf (f , "," );
49+ }
50+ }
51+ }
52+
53+ /**
54+ * Writes an array of integers to a file as a series of comma-separated values.
55+ *
56+ * @param f file to write to
57+ * @param x array of integers
58+ * @param len array length
59+ */
60+ void write_array_ui32 (FILE * f , const unsigned int * x , const unsigned int len ) {
61+ unsigned int i ;
62+
63+ for (i = 0 ; i < len ; i ++ ) {
64+ fprintf (f , "%u" , x [i ]);
65+ if (i < len - 1 ) {
66+ fprintf (f , "," );
67+ }
68+ }
69+ }
70+
71+ /**
72+ * Writes a named array of doubles to a file as JSON.
73+ *
74+ * @param f file to write to
75+ * @param name array name
76+ * @param x data
77+ * @param len array length
78+ */
79+ void write_named_array_f64 (FILE * f , const char * name , const double * x , const unsigned int len ) {
80+ fprintf (f , "\"%s\":[" , name );
81+ write_array_f64 (f , x , len );
82+ fprintf (f , "]" );
83+ }
84+
85+ /**
86+ * Writes a named array of integers to a file as JSON.
87+ *
88+ * @param f file to write to
89+ * @param name array name
90+ * @param x data
91+ * @param len array length
92+ */
93+ void write_named_array_ui32 (FILE * f , const char * name , const unsigned int * x , const unsigned int len ) {
94+ fprintf (f , "\"%s\":[" , name );
95+ write_array_ui32 (f , x , len );
96+ fprintf (f , "]" );
97+ }
98+
99+ /**
100+ * Writes data to a file as JSON.
101+ *
102+ * @param f file to write to
103+ * @param y results
104+ * @param len array length
105+ */
106+ void write_data_as_json_ui32 (FILE * f , const unsigned int * y , const unsigned int len ) {
107+ fprintf (f , "{" );
108+ write_named_array_ui32 (f , "expected" , y , len );
109+ fprintf (f , "}" );
110+ }
111+
112+ /**
113+ * Writes data to a file as JSON.
114+ *
115+ * @param f file to write to
116+ * @param y results
117+ * @param len array length
118+ */
119+ void write_data_as_json_f64 (FILE * f , const double * y , const unsigned int len ) {
120+ fprintf (f , "{" );
121+ write_named_array_f64 (f , "expected" , y , len );
122+ fprintf (f , "}" );
123+ }
124+
125+ /**
126+ * Generates test fixtures for uint32 values with integer seed.
127+ *
128+ * @param seed PRNG seed
129+ * @param len number of output values
130+ * @param name output filename
131+ */
132+ void generate_ui32_integer_seed (const uint32_t seed , const unsigned int len , const char * name ) {
133+ tinymt32_t state ;
134+ unsigned int * y ;
135+ unsigned int i ;
136+ FILE * f ;
137+
138+ // Allocate an output array:
139+ y = (unsigned int * ) malloc (len * sizeof (unsigned int ));
140+ if (y == NULL ) {
141+ printf ("Error allocating memory.\n" );
142+ exit (1 );
143+ }
144+
145+ // Set TinyMT parameters (these are default values from the original implementation)
146+ state .mat1 = 0x8f7011ee ;
147+ state .mat2 = 0xfc78ff1f ;
148+ state .tmat = 0x3793fdff ;
149+
150+ // Initialize the PRNG:
151+ tinymt32_init (& state , seed );
152+
153+ // Generate fixture data:
154+ for (i = 0 ; i < len ; i ++ ) {
155+ y [i ] = tinymt32_generate_uint32 (& state );
156+ }
157+
158+ // Open a new file:
159+ f = fopen (name , "w" );
160+ if (f == NULL ) {
161+ printf ("Error opening file.\n" );
162+ exit (1 );
163+ }
164+
165+ // Write data as JSON:
166+ write_data_as_json_ui32 (f , y , len );
167+
168+ // Close the file:
169+ fclose (f );
170+
171+ // Free allocated memory:
172+ free (y );
173+ }
174+
175+ /**
176+ * Generates test fixtures for float64 values with integer seed.
177+ *
178+ * @param seed PRNG seed
179+ * @param len number of output values
180+ * @param name output filename
181+ */
182+ void generate_f64_integer_seed (const uint32_t seed , const unsigned int len , const char * name ) {
183+ tinymt32_t state ;
184+ double * y ;
185+ unsigned int i ;
186+ FILE * f ;
187+
188+ // Allocate an output array:
189+ y = (double * ) malloc (len * sizeof (double ));
190+ if (y == NULL ) {
191+ printf ("Error allocating memory.\n" );
192+ exit (1 );
193+ }
194+
195+ // Set TinyMT parameters (these are default values from the original implementation)
196+ state .mat1 = 0x8f7011ee ;
197+ state .mat2 = 0xfc78ff1f ;
198+ state .tmat = 0x3793fdff ;
199+
200+ // Initialize the PRNG:
201+ tinymt32_init (& state , seed );
202+
203+ // Generate fixture data:
204+ for (i = 0 ; i < len ; i ++ ) {
205+ y [i ] = tinymt32_generate_double (& state );
206+ }
207+
208+ // Open a new file:
209+ f = fopen (name , "w" );
210+ if (f == NULL ) {
211+ printf ("Error opening file.\n" );
212+ exit (1 );
213+ }
214+
215+ // Write data as JSON:
216+ write_data_as_json_f64 (f , y , len );
217+
218+ // Close the file:
219+ fclose (f );
220+
221+ // Free allocated memory:
222+ free (y );
223+ }
224+
225+ /**
226+ * Main execution sequence.
227+ */
228+ int main (void ) {
229+ unsigned int len ;
230+
231+ // Define the array length:
232+ len = 1000 ;
233+
234+ // Generate fixture data:
235+ generate_ui32_integer_seed (1234 , len , "uint32_integer_seed.json" );
236+ generate_f64_integer_seed (1234 , len , "float64_integer_seed.json" );
237+
238+ return 0 ;
239+ }
0 commit comments