more (failing) tests

This commit is contained in:
Andreas Zwinkau 2015-01-14 22:40:31 +01:00 committed by Andreas Zwinkau
parent 8983dc8b15
commit 718f3a5331
6 changed files with 181 additions and 0 deletions

31
tests/guessnumber.d Normal file
View File

@ -0,0 +1,31 @@
import std.stdio, std.random, std.typecons, std.conv, std.string,
std.range;
void main() {
immutable interval = tuple(1, 100);
writefln("Guess my target number that is between " ~
"%d and %d (inclusive).\n", interval[]);
immutable target = uniform!"[]"(interval[]);
foreach (immutable i; sequence!q{n}) {
writef("Your guess #%d: ", i + 1);
immutable txt = stdin.readln.strip;
Nullable!int answer;
try {
answer = txt.to!int;
} catch (ConvException e) {
writefln(" I don't understand your input '%s'", txt);
continue;
}
if (answer < interval[0] || answer > interval[1]) {
writeln(" Out of range!");
continue;
}
if (answer == target) {
writeln(" Well guessed.");
break;
}
writeln(answer < target ? " Too low." : " Too high.");
}
}

36
tests/guessnumber.d.ref Normal file
View File

@ -0,0 +1,36 @@
import std.stdio, std.random, std.typecons, std.conv, std.string, std.range;
void main()
{
immutable interval = tuple(1, 100);
writefln("Guess my target number that is between "
~ "%d and %d (inclusive).\n",
interval[]);
immutable target = uniform!"[]"(interval[]);
foreach (immutable i; sequence!q{n})
{
writef("Your guess #%d: ", i + 1);
immutable txt = stdin.readln.strip;
Nullable!int answer;
try
{
answer = txt.to!int;
}
catch(ConvException e)
{
writefln(" I don't understand your input '%s'", txt);
continue;
}
if (answer < interval[0] || answer > interval[1])
{
writeln(" Out of range!");
continue;
}
if (answer == target)
{
writeln(" Well guessed.");
break;
}
writeln(answer < target ? " Too low.":" Too high.");
}
}

45
tests/heronian.d Normal file
View File

@ -0,0 +1,45 @@
import std.stdio, std.math, std.range, std.algorithm, std.numeric, std.traits, std.typecons;
double hero(in uint a, in uint b, in uint c) pure nothrow @safe @nogc {
immutable s = (a + b + c) / 2.0;
immutable a2 = s * (s - a) * (s - b) * (s - c);
return (a2 > 0) ? a2.sqrt : 0.0;
}
bool isHeronian(in uint a, in uint b, in uint c) pure nothrow @safe @nogc {
immutable h = hero(a, b, c);
return h > 0 && h.floor == h.ceil;
}
T gcd3(T)(in T x, in T y, in T z) pure nothrow @safe @nogc {
return gcd(gcd(x, y), z);
}
void main() /*@safe*/ {
enum uint maxSide = 200;
// Sort by increasing area, perimeter, then sides.
//auto h = cartesianProduct!3(iota(1, maxSide + 1))
auto r = iota(1, maxSide + 1);
const h = cartesianProduct(r, r, r)
//.filter!({a, b, c} => ...
.filter!(t => t[0] <= t[1] && t[1] <= t[2] &&
t[0] + t[1] > t[2] &&
t[].gcd3 == 1 && t[].isHeronian)
.array
.schwartzSort!(t => tuple(t[].hero, t[].only.sum, t.reverse))
.release;
static void showTriangles(R)(R ts) @safe {
"Area Perimeter Sides".writeln;
foreach (immutable t; ts)
writefln("%3s %8d %3dx%dx%d", t[].hero, t[].only.sum, t[]);
}
writefln("Primitive Heronian triangles with sides up to %d: %d", maxSide, h.length);
"\nFirst ten when ordered by increasing area, then perimeter,then maximum sides:".writeln;
showTriangles(h.take(10));
"\nAll with area 210 subject to the previous ordering:".writeln;
showTriangles(h.filter!(t => t[].hero == 210));
}

48
tests/heronian.d.ref Normal file
View File

@ -0,0 +1,48 @@
import std.stdio, std.math, std.range, std.algorithm, std.numeric, std.traits,
std.typecons;
double hero(in uint a, in uint b, in uint c) pure nothrow @safe@nogc
{
immutable s = (a + b + c) / 2.0;
immutable a2 = s * (s - a) * (s - b) * (s - c);
return (a2 > 0) ? a2.sqrt : 0.0;
}
bool isHeronian(in uint a, in uint b, in uint c) pure nothrow @safe @nogc
{
immutable h = hero(a, b, c);
return h > 0 && h.floor == h.ceil;
}
T gcd3(T)(in T x, in T y, in T z) pure nothrow @safe @nogc
{
return gcd(gcd(x, y), z);
}
void main() /*@safe*/
{
enum uint maxSide = 200;
// Sort by increasing area, perimeter, then sides.
//auto h = cartesianProduct!3(iota(1, maxSide + 1))
auto r = iota(1, maxSide + 1);
const h = cartesianProduct(r, r, r)
//.filter!({a, b, c} => ...
.filter!(t => t[0] <= t[1] && t[1] <= t[2] && t[0] + t[1] > t[2] &&
t[].gcd3 == 1 && t[].isHeronian)
.array.schwartzSort!(t => tuple(t[].hero, t[].only.sum, t.reverse))
.release;
static void showTriangles(R)(R ts) @safe
{
"Area Perimeter Sides".writeln;
foreach (immutable t; ts)
writefln("%3s %8d %3dx%dx%d", t[].hero, t[].only.sum, t[]);
}
writefln("Primitive Heronian triangles with sides up to %d: %d", maxSide,
h.length);
"\nFirst ten when ordered by increasing area, then perimeter,then maximum sides:".writeln;
showTriangles(h.take(10));
"\nAll with area 210 subject to the previous ordering:".writeln;
showTriangles(h.filter!(t => t[].hero == 210));
}

9
tests/higherorder.d Normal file
View File

@ -0,0 +1,9 @@
int hof(int a, int b, int delegate(int, int) f) {
return f(a, b);
}
void main() {
import std.stdio;
writeln("Add: ", hof(2, 3, (a, b) => a + b));
writeln("Multiply: ", hof(2, 3, (a, b) => a * b));
}

12
tests/higherorder.d.ref Normal file
View File

@ -0,0 +1,12 @@
int hof(int a, int b, int delegate(int, int) f)
{
return f(a, b);
}
void main()
{
import std.stdio;
writeln("Add: ", hof(2, 3, (a, b) => a + b));
writeln("Multiply: ", hof(2, 3, (a, b) => a * b));
}