1 /*
2     Copyright (c) 2005-2021 Intel Corporation
3 
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7 
8         http://www.apache.org/licenses/LICENSE-2.0
9 
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15 */
16 
17 #include <cstdlib>
18 #include <cstdio>
19 
20 #include <stdexcept>
21 
22 #if _WIN32
23 #include <io.h>
24 #ifndef F_OK
25 #define F_OK 0
26 #endif
27 #define access _access
28 #else
29 #include <unistd.h>
30 #endif
31 
32 const long INPUT_SIZE = 1000000;
33 
34 //! Generates sample input for square.cpp
gen_input(const char * fname)35 void gen_input(const char *fname) {
36     long num = INPUT_SIZE;
37     FILE *fptr = fopen(fname, "w");
38     if (!fptr) {
39         throw std::runtime_error("Could not open file for generating input");
40     }
41 
42     int a = 0;
43     int b = 1;
44     for (long j = 0; j < num; ++j) {
45         fprintf(fptr, "%u\n", a);
46         b += a;
47         a = (b - a) % 10000;
48         if (a < 0)
49             a = -a;
50     }
51 
52     if (fptr) {
53         fclose(fptr);
54     }
55 }
56 
generate_if_needed(const char * fname)57 void generate_if_needed(const char *fname) {
58     if (access(fname, F_OK) != 0)
59         gen_input(fname);
60 }
61