#!/usr/bin/env julia
#
# @license Apache-2.0
#
# Copyright (c) 2018 The Stdlib Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import JSON

"""
    gen( x, filepath )

Generate fixture data and write to file.

# Arguments

* `x`: domain
* `name::AbstractString`: filepath of the output file

# Examples

``` julia
julia> x = linspace( -0.5, 1.5, 2001 );
julia> gen( x, \"./data.json\" );
```
"""
function gen( x, filepath )
	y = Array( Float64, length( x ) );
	for i in eachindex(x)
		y[i] = erfcinv( x[i] );
	end

	data = Dict([
		("x", x),
		("expected", y)
	]);

	outfile = open( filepath, "w" );
	write( outfile, JSON.json(data) );
	close( outfile );
end

# Get the filename:
file = @__FILE__;

# Extract the directory in which this file resides:
dir = dirname( file );

# 0.5 <= x <= 1.5 (linear regime)
x = linspace( 0.5, 1.5, 3000 );
out = joinpath( dir, "x_0.5_1.5.json" );
gen( x, out );

# 0.25 < 2-x < 0.5
x = linspace( 1.5, 1.75, 500 );
out = joinpath( dir, "x_1.5_1.75.json" );
gen( x, out );

# 0.25 < x < 0.5
x = linspace( 0.25, 0.5, 500 );
out = joinpath( dir, "x_0.25_0.5.json" );
gen( x, out );

# 0.25 < 2-x < 0.00012340980408664937
x = linspace( 1.75, 1.9998, 500 );
out = joinpath( dir, "x_1.75_1.9998.json" );
gen( x, out );

# 0.00012340980408664937 < x < 0.25
x = linspace( 0.0002, 0.25, 500 );
out = joinpath( dir, "x_0.0002_0.25.json" );
gen( x, out );

# 0.00012340980408664937 < 2-x < 2.220446049250313e-16
x = linspace( 1.9998, 1.9999999999999998, 500 );
out = joinpath( dir, "x_1.9998_1.9999..8.json" );
gen( x, out );

# 2.220446049250313e-16 < 2-x < 0
x = linspace( 1.9999999999999998, 2, 500 );
out = joinpath( dir, "x_1.9999..8_2.json" );
gen( x, out );
