|
| 1 | +#!/usr/bin/env julia |
| 2 | +# |
| 3 | +# @license Apache-2.0 |
| 4 | +# |
| 5 | +# Copyright (c) 2018 The Stdlib Authors. |
| 6 | +# |
| 7 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +# you may not use this file except in compliance with the License. |
| 9 | +# You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, software |
| 14 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +# See the License for the specific language governing permissions and |
| 17 | +# limitations under the License. |
| 18 | + |
| 19 | +import JSON |
| 20 | + |
| 21 | +""" |
| 22 | + gen( domain, name ) |
| 23 | +
|
| 24 | +Generate fixture data and write to file. |
| 25 | +
|
| 26 | +# Arguments |
| 27 | +
|
| 28 | +* `domain`: domain |
| 29 | +* `name::AbstractString`: output filename |
| 30 | +
|
| 31 | +# Examples |
| 32 | +
|
| 33 | +``` julia |
| 34 | +julia> x = range( -708, stop = 709, length = 2001 ); |
| 35 | +julia> gen( x, \"data.json\" ); |
| 36 | +``` |
| 37 | +""" |
| 38 | +function gen( domain, name ) |
| 39 | + x = collect( domain ); |
| 40 | + y = cosc.( x ); |
| 41 | + |
| 42 | + # Store data to be written to file as a collection: |
| 43 | + data = Dict([ |
| 44 | + ("x", x), |
| 45 | + ("expected", y) |
| 46 | + ]); |
| 47 | + |
| 48 | + # Based on the script directory, create an output filepath: |
| 49 | + filepath = joinpath( dir, name ); |
| 50 | + |
| 51 | + # Write the data to the output filepath as JSON: |
| 52 | + outfile = open( filepath, "w" ); |
| 53 | + write( outfile, JSON.json(data) ); |
| 54 | + write( outfile, "\n" ); |
| 55 | + close( outfile ); |
| 56 | +end |
| 57 | + |
| 58 | +# Get the filename: |
| 59 | +file = @__FILE__; |
| 60 | + |
| 61 | +# Extract the directory in which this file resides: |
| 62 | +dir = dirname( file ); |
| 63 | + |
| 64 | +# Generate fixture data for decimal values: |
| 65 | +x = range( -100, stop = 100, length = 4003 ); |
| 66 | +gen( x, "data.json" ); |
| 67 | + |
| 68 | +# Large negative values: |
| 69 | +x = range( -709.0895, stop = -100, length = 1003 ); |
| 70 | +gen( x, "large_negative.json" ); |
| 71 | + |
| 72 | +# Large positive values: |
| 73 | +x = range( 100, stop = 710.475, length = 1003 ); |
| 74 | +gen( x, "large_positive.json" ); |
| 75 | + |
| 76 | +# Tiny negative values: |
| 77 | +x = range( -1e-200, stop = -1e-208, length = 503 ); |
| 78 | +gen( x, "tiny_negative.json" ); |
| 79 | + |
| 80 | +# Tiny positive values: |
| 81 | +x = range( 1e-300, stop = 1e-308, length = 503 ); |
| 82 | +gen( x, "tiny_positive.json" ); |
0 commit comments