1
exercism fetch d robot-name

source/robot_name.d

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

module robot;

import std.regex;
import std.stdio;

unittest
{

// test for properly formatted name
{
	auto pattern = regex(`^[A-Z]{2}\d{3}`);
	auto theRobot = new Robot();

	// test the regex pattern
	assert(matchAll("VAV224", pattern).empty);
	assert(matchAll("V221", pattern).empty);
	assert(matchAll("190", pattern).empty);
	assert(matchAll("12345", pattern).empty);
	assert(matchAll("SB1", pattern).empty);
	assert(matchAll("TT", pattern).empty);

	writefln("Robot name: %s", theRobot.name);

	// test that the name respects the pattern
	// that is: "2 uppercase letters followed by 3 digits"
	assert(!matchAll(theRobot.name, pattern).empty);
}

immutable int allTestsEnabled = 0;

static if (allTestsEnabled)
{
// test name stickiness
{
	auto theRobot = new Robot();
	auto name = theRobot.name;

	writefln("Robot name: %s", theRobot.name);
	assert(name == theRobot.name);
}

// test different names for different Robots
{
	auto erTwoDeeTwo = new Robot();
	auto beeBeeEight = new Robot();

	writefln("Robot name: %s", erTwoDeeTwo.name);
	writefln("Robot name: %s", beeBeeEight.name);
	assert(erTwoDeeTwo.name != beeBeeEight.name);
}

// test name reset
{
	auto theRobot = new Robot();
	auto nameOne = theRobot.name;
	theRobot.reset();
	auto nameTwo = theRobot.name;

	writefln("Robot name: %s", nameOne);
	writefln("Robot name: %s", nameTwo);
	assert(nameOne != nameTwo);
}

// collision test
{
	foreach(i; 1..10000)
	{
		auto theRobot = new Robot();
	}

	writefln("Collisons: %s that is %s%%", Robot.collisons, (Robot.collisons/10000.0f) * 100);
}
}

}