Skip to content

Commit

Permalink
Initialized C# code for exerciese.
Browse files Browse the repository at this point in the history
  • Loading branch information
wubin28 committed Apr 14, 2014
1 parent ca788e1 commit 8f6d459
Show file tree
Hide file tree
Showing 12 changed files with 382 additions and 0 deletions.
@@ -0,0 +1,68 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.50727</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{BBCF3C67-8E31-48E3-AD2E-8C605E2E07BE}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>TDDMicroExercises</RootNamespace>
<AssemblyName>TDDMicroExercises</AssemblyName>
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
<FileUpgradeFlags>
</FileUpgradeFlags>
<OldToolsVersion>2.0</OldToolsVersion>
<UpgradeBackupLocation />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="nunit.framework, Version=2.4.3.0, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>lib\nunit.framework.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Web" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="TelemetrySystem\TelemetryClient.cs" />
<Compile Include="TelemetrySystem.Tests\TelemetryDiagnosticControlsTest.cs" />
<Compile Include="TelemetrySystem\TelemetryDiagnosticControls.cs" />
<Compile Include="TirePressureMonitoringSystem\Alarm.cs" />
<Compile Include="TirePressureMonitoringSystem\Sensor.cs" />
<Compile Include="TurnTicketDispenser\TicketDispenser.cs" />
<Compile Include="TurnTicketDispenser\TurnNumberSequence.cs" />
<Compile Include="TurnTicketDispenser\TurnTicket.cs" />
<Compile Include="UnicodeFileToHtmlTextConverter\UnicodeFileToHtmlTextConverter.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
@@ -0,0 +1,20 @@

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TDDMicroExercises", "TDDMicroExercises.csproj", "{BBCF3C67-8E31-48E3-AD2E-8C605E2E07BE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{BBCF3C67-8E31-48E3-AD2E-8C605E2E07BE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BBCF3C67-8E31-48E3-AD2E-8C605E2E07BE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BBCF3C67-8E31-48E3-AD2E-8C605E2E07BE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BBCF3C67-8E31-48E3-AD2E-8C605E2E07BE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
@@ -0,0 +1,14 @@
using NUnit.Framework;

namespace TDDMicroExercises.TelemetrySystem.Tests
{
[TestFixture]
public class TelemetryDiagnosticControlsTest
{
[Test]
public void CheckTransmission_should_send_a_diagnostic_message_and_receive_a_status_message_response()
{
}

}
}
@@ -0,0 +1,101 @@
using System;

namespace TDDMicroExercises.TelemetrySystem
{
public class TelemetryClient
{
//
// The communication with the server is simulated in this implementation.
// Because the focus of the exercise is on the other class.
//

public const string DiagnosticMessage = "AT#UD";

private bool _onlineStatus;
private bool _diagnosticMessageJustSent = false;

private readonly Random _connectionEventsSimulator = new Random();
private readonly Random _randomMessageSimulator = new Random();

public bool OnlineStatus
{
get { return _onlineStatus; }
}

public void Connect(string telemetryServerConnectionString)
{
if (string.IsNullOrEmpty(telemetryServerConnectionString))
{
throw new ArgumentNullException();
}

// Fake the connection with 20% chances of success
bool success = _connectionEventsSimulator.Next(1, 10) <= 2;

_onlineStatus = success;

}

public void Disconnect()
{
_onlineStatus = false;
}

public void Send(string message)
{
if (string.IsNullOrEmpty(message))
{
throw new ArgumentNullException();
}

// The simulation of Send() actually just remember if the last message sent was a diagnostic message.
// This information will be used to simulate the Receive(). Indeed there is no real server listening.
if (message == DiagnosticMessage)
{
_diagnosticMessageJustSent = true;
}
else
{
_diagnosticMessageJustSent = false;
}
}

public string Receive()
{
string message;

if (_diagnosticMessageJustSent)
{
// Simulate the reception of the diagnostic message
message = "LAST TX rate................ 100 MBPS\r\n"
+ "HIGHEST TX rate............. 100 MBPS\r\n"
+ "LAST RX rate................ 100 MBPS\r\n"
+ "HIGHEST RX rate............. 100 MBPS\r\n"
+ "BIT RATE.................... 100000000\r\n"
+ "WORD LEN.................... 16\r\n"
+ "WORD/FRAME.................. 511\r\n"
+ "BITS/FRAME.................. 8192\r\n"
+ "MODULATION TYPE............. PCM/FM\r\n"
+ "TX Digital Los.............. 0.75\r\n"
+ "RX Digital Los.............. 0.10\r\n"
+ "BEP Test.................... -5\r\n"
+ "Local Rtrn Count............ 00\r\n"
+ "Remote Rtrn Count........... 00";

_diagnosticMessageJustSent = false;
}
else
{
// Simulate the reception of a response message returning a random message.
message = string.Empty;
int messageLength = _randomMessageSimulator.Next(50, 110);
for(int i = messageLength; i > 0; --i)
{
message += (char)_randomMessageSimulator.Next(40, 126);
}
}

return message;
}
}
}
@@ -0,0 +1,46 @@

using System;

namespace TDDMicroExercises.TelemetrySystem
{
public class TelemetryDiagnosticControls
{
private const string DiagnosticChannelConnectionString = "*111#";

private readonly TelemetryClient _telemetryClient;
private string _diagnosticInfo = string.Empty;

public TelemetryDiagnosticControls()
{
_telemetryClient = new TelemetryClient();
}

public string DiagnosticInfo
{
get { return _diagnosticInfo; }
set { _diagnosticInfo = value; }
}

public void CheckTransmission()
{
_diagnosticInfo = string.Empty;

_telemetryClient.Disconnect();

int retryLeft = 3;
while (_telemetryClient.OnlineStatus == false && retryLeft > 0)
{
_telemetryClient.Connect(DiagnosticChannelConnectionString);
retryLeft -= 1;
}

if(_telemetryClient.OnlineStatus == false)
{
throw new Exception("Unable to connect.");
}

_telemetryClient.Send(TelemetryClient.DiagnosticMessage);
_diagnosticInfo = _telemetryClient.Receive();
}
}
}
@@ -0,0 +1,28 @@
namespace TDDMicroExercises.TirePressureMonitoringSystem
{
public class Alarm
{
private const double LowPressureThreshold = 17;
private const double HighPressureThreshold = 21;

readonly Sensor _sensor = new Sensor();

bool _alarmOn = false;

public void Check()
{
double psiPressureValue = _sensor.PopNextPressurePsiValue();

if (psiPressureValue < LowPressureThreshold || HighPressureThreshold < psiPressureValue)
{
_alarmOn = true;
}
}

public bool AlarmOn
{
get { return _alarmOn; }
}

}
}
@@ -0,0 +1,28 @@
using System;

namespace TDDMicroExercises.TirePressureMonitoringSystem
{
public class Sensor
{
//
// 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,0 +1,13 @@
namespace TDDMicroExercises.TurnTicketDispenser
{
public class TicketDispenser
{
public TurnTicket GetTurnTicket()
{
int newTurnNumber = TurnNumberSequence.GetNextTurnNumber();
var newTurnTicket = new TurnTicket(newTurnNumber);

return newTurnTicket;
}
}
}
@@ -0,0 +1,12 @@
namespace TDDMicroExercises.TurnTicketDispenser
{
public static class TurnNumberSequence
{
private static int _turnNumber = 0;

public static int GetNextTurnNumber()
{
return _turnNumber++;
}
}
}
@@ -0,0 +1,18 @@
namespace TDDMicroExercises.TurnTicketDispenser
{
public class TurnTicket
{
private readonly int _turnNumber;

public TurnTicket(int turnNumber)
{
_turnNumber = turnNumber;
}

public int TurnNumber
{
get { return _turnNumber; }
}

}
}
@@ -0,0 +1,34 @@
using System.IO;
using System.Web;

namespace TDDMicroExercises.UnicodeFileToHtmlTextConverter
{
public class UnicodeFileToHtmlTextConverter
{
private readonly string _fullFilenameWithPath;


public UnicodeFileToHtmlTextConverter(string fullFilenameWithPath)
{
_fullFilenameWithPath = fullFilenameWithPath;
}

public string ConvertToHtml()
{
using (TextReader unicodeFileStream = File.OpenText(_fullFilenameWithPath))
{
string html = string.Empty;

string line = unicodeFileStream.ReadLine();
while (line != null)
{
html += HttpUtility.HtmlEncode(line);
html += "<br />";
line = unicodeFileStream.ReadLine();
}

return html;
}
}
}
}
Binary file not shown.

0 comments on commit 8f6d459

Please sign in to comment.