Skip to content

Instantly share code, notes, and snippets.

@JamesNK
Created March 24, 2017 00:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JamesNK/4e6bee576196be016fdd31a3dbe0880e to your computer and use it in GitHub Desktop.
Save JamesNK/4e6bee576196be016fdd31a3dbe0880e to your computer and use it in GitHub Desktop.
Json.NET double tester - Program.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace ConsoleApp1
{
class Program
{
private static bool Exit;
static void Main(string[] args)
{
Console.WriteLine("Hi!");
var t = Task.Run(TestDoubles);
Console.ReadKey();
Exit = true;
t.Wait();
}
public static double NextDouble(
Random random,
double minValue,
double maxValue)
{
return random.NextDouble() * (maxValue - minValue) + minValue;
}
private static Task TestDoubles()
{
Random random = new Random();
while (!Exit)
{
int a = random.Next(0, int.MaxValue);
int b = random.Next(0, int.MaxValue);
bool negative = random.NextDouble() >= 0.5;
string doubleText = ((negative) ? "-" : string.Empty) + a + "." + b;
StringReader sr = new StringReader(doubleText);
JsonTextReader reader = new JsonTextReader(sr);
double? d = reader.ReadAsDouble();
double expected = double.Parse(doubleText);
if (d.Value != expected)
{
File.AppendAllText("incorrect.txt", doubleText + Environment.NewLine);
}
}
return Task.FromResult(true);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment