Skip to content
This repository has been archived by the owner on Apr 29, 2020. It is now read-only.

Commit

Permalink
TirePressureMonitoringSystem/C#: deleted unnecessary code and synchro…
Browse files Browse the repository at this point in the history
…nised the proposed solution with changes in the original exercise.
  • Loading branch information
Luca Minudel committed Sep 12, 2013
1 parent 367594a commit 83486de
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 30 deletions.
Expand Up @@ -8,9 +8,9 @@ public class AlarmTest
[Test]
public void a_normal_pressure_value_should_not_raise_the_alarm()
{
StubSensor stubSensor = new StubSensor();
var stubSensor = new StubSensor();
stubSensor.StubCallToPopNextPressurePsiValue(Alarm.LowPressureTreshold);
Alarm target = new Alarm(stubSensor);
var target = new Alarm(stubSensor);

target.Check();

Expand All @@ -20,9 +20,9 @@ public void a_normal_pressure_value_should_not_raise_the_alarm()
[Test]
public void a_pressure_value_out_of_range_should_raise_the_alarm()
{
StubSensor stubSensor = new StubSensor();
var stubSensor = new StubSensor();
stubSensor.StubCallToPopNextPressurePsiValue(Alarm.LowPressureTreshold -1);
Alarm target = new Alarm(stubSensor);
var target = new Alarm(stubSensor);

target.Check();

Expand All @@ -32,9 +32,9 @@ public void a_pressure_value_out_of_range_should_raise_the_alarm()
[Test]
public void a_normal_pressure_value_after_an_out_of_range_pressure_value_should_keep_the_alarm_on()
{
StubSensor stubSensor = new StubSensor();
stubSensor.StubCallToPopNextPressurePsiValues(new double[] { Alarm.LowPressureTreshold, Alarm.LowPressureTreshold - 1, Alarm.LowPressureTreshold });
Alarm target = new Alarm(stubSensor);
var stubSensor = new StubSensor();
stubSensor.StubCallToPopNextPressurePsiValues(new[] { Alarm.LowPressureTreshold, Alarm.LowPressureTreshold - 1, Alarm.LowPressureTreshold });
var target = new Alarm(stubSensor);

target.Check();
target.Check();
Expand Down
Expand Up @@ -9,7 +9,7 @@ public class StubSensor : ISensor

public void StubCallToPopNextPressurePsiValue(double returnedValue)
{
StubCallToPopNextPressurePsiValues(new double[] { returnedValue });
StubCallToPopNextPressurePsiValues(new[] { returnedValue });
}

public void StubCallToPopNextPressurePsiValues(double[] returnedValues)
Expand Down
Expand Up @@ -3,13 +3,11 @@ namespace TDDMicroExercises.OneSolution.TirePressureMonitoringSystem
public class Alarm
{
public const double LowPressureTreshold = 17;
public const double HighPressureTreshold = 21;

ISensor _sensor;
public const double HighPressureTreshold = 21;

readonly ISensor _sensor;

bool _alarmOn = false;
private long _alarmCount = 0;


public Alarm() : this(new Sensor())
{
Expand All @@ -27,7 +25,6 @@ public void Check()
if (psiPressureValue < LowPressureTreshold || HighPressureTreshold < psiPressureValue)
{
_alarmOn = true;
_alarmCount += 1;
}
}

Expand Down
Expand Up @@ -3,22 +3,26 @@
namespace TDDMicroExercises.OneSolution.TirePressureMonitoringSystem
{
public class Sensor : ISensor
{
const double Offset = 16;

public double PopNextPressurePsiValue()
{
double pressureTelemetryValue;
SamplePressure(out pressureTelemetryValue);

return Offset + pressureTelemetryValue;
}

private static void SamplePressure(out double pressureTelemetryValue)
{
// placeholder implementation that simulate a real sensor in a real tire
Random basicRandomNumbersGenerator = new Random(42);
pressureTelemetryValue = 6 * basicRandomNumbersGenerator.NextDouble() * basicRandomNumbersGenerator.NextDouble();
{
//
// The reading of the pressure value from the sensor is simulated in this implementation.
// Because the focus of the exercise is on the other class.
//

const double Offset = 16;
readonly Random _randomPressureSampleSimulator = new Random();

public double PopNextPressurePsiValue()
{
double pressureTelemetryValue = ReadPressureSample();

return Offset + pressureTelemetryValue;
}

private double ReadPressureSample()
{
// Simulate info read from a real sensor in a real tire
return 6 * _randomPressureSampleSimulator.NextDouble() * _randomPressureSampleSimulator.NextDouble();
}
}
}

0 comments on commit 83486de

Please sign in to comment.