From 6332104c39b92174066d818e40dc5e68bcc449d5 Mon Sep 17 00:00:00 2001 From: Ryan Potts Date: Wed, 13 Feb 2019 19:45:06 -0500 Subject: [PATCH] Resistor-Color: New Exercise --- config.json | 10 +++ exercises/resistor-color/README.md | 47 +++++++++++ exercises/resistor-color/ResistorColor.dpr | 60 ++++++++++++++ .../resistor-color/uResistorColorTests.pas | 81 +++++++++++++++++++ exercises/resistor-color/uResistorExample.pas | 31 +++++++ 5 files changed, 229 insertions(+) create mode 100644 exercises/resistor-color/README.md create mode 100644 exercises/resistor-color/ResistorColor.dpr create mode 100644 exercises/resistor-color/uResistorColorTests.pas create mode 100644 exercises/resistor-color/uResistorExample.pas diff --git a/config.json b/config.json index f9bc8d6b..045ad05d 100644 --- a/config.json +++ b/config.json @@ -223,6 +223,16 @@ "transforming" ] }, + { + "slug": "resistor-color", + "uuid": "cd21de38-d572-4cc1-9a6f-1c5aed511b32", + "core": false, + "unlocked_by": "two-fer", + "difficulty": 1, + "topics": [ + "arrays" + ] + }, { "slug": "collatz-conjecture", "uuid": "edfc2903-1165-4029-9901-0be72b32865f", diff --git a/exercises/resistor-color/README.md b/exercises/resistor-color/README.md new file mode 100644 index 00000000..7a38da3c --- /dev/null +++ b/exercises/resistor-color/README.md @@ -0,0 +1,47 @@ +# Resistor Color + +Resistors have color coded bands, where each color maps to a number. The first 2 bands of a resistor have a simple encoding scheme: each color maps to a single number. + +These colors are encoded as follows: + +- Black: 0 +- Brown: 1 +- Red: 2 +- Orange: 3 +- Yellow: 4 +- Green: 5 +- Blue: 6 +- Violet: 7 +- Grey: 8 +- White: 9 + +Mnemonics map the colors to the numbers, that, when stored as an array, happen to map to their index in the array: Better Be Right Or Your Great Big Values Go Wrong. + +More information on the color encoding of resistors can be found in the [Electronic color code Wikipedia article](https://en.wikipedia.org/wiki/Electronic_color_code) + +## Testing + +In order to run the tests for this track, you will need to install +DUnitX. Please see the [installation](http://www.exercism.io/languages/delphi/installation) instructions for more information. + +### Loading Exercises into Delphi + +If Delphi is properly installed, and `*.dpr` file types have been associated with Delphi, then double clicking the supplied `*.dpr` file will start Delphi and load the exercise/project. `control + F9` is the keyboard shortcut to compile the project or pressing `F9` will compile and run the project. + +Alternatively you may opt to start Delphi and load your project via. the `File` drop down menu. + +### When Questions Come Up +We monitor the [Pascal-Delphi](https://gitter.im/exercism/Pascal-Delphi) support room on [gitter.im](https://gitter.im) to help you with any questions that might arise. + +### Submitting Exercises + +Note that, when trying to submit an exercise, make sure the exercise file you're submitting is in the `exercism/delphi/` directory. + +For example, if you're submitting `ubob.pas` for the Bob exercise, the submit command would be something like `exercism submit /delphi/bob/ubob.pas`. + +## Source + +Maud de Vries, Erik Schierboom [https://github.com/exercism/problem-specifications/issues/1458](https://github.com/exercism/problem-specifications/issues/1458) + +## Submitting Incomplete Solutions +It's possible to submit an incomplete solution so you may receive assistance from a mentor. diff --git a/exercises/resistor-color/ResistorColor.dpr b/exercises/resistor-color/ResistorColor.dpr new file mode 100644 index 00000000..8cf2c7f4 --- /dev/null +++ b/exercises/resistor-color/ResistorColor.dpr @@ -0,0 +1,60 @@ +program ResistorColor; + +{$IFNDEF TESTINSIGHT} +{$APPTYPE CONSOLE} +{$ENDIF}{$STRONGLINKTYPES ON} +uses + System.SysUtils, + {$IFDEF TESTINSIGHT} + TestInsight.DUnitX, + {$ENDIF } + DUnitX.Loggers.Console, + DUnitX.Loggers.Xml.NUnit, + DUnitX.TestFramework, + uResistorColorTests in 'uResistorColorTests.pas', + uResistor in 'uResistor.pas'; + +var + runner : ITestRunner; + results : IRunResults; + logger : ITestLogger; + nunitLogger : ITestLogger; +begin +{$IFDEF TESTINSIGHT} + TestInsight.DUnitX.RunRegisteredTests; + exit; +{$ENDIF} + try + //Check command line options, will exit if invalid + TDUnitX.CheckCommandLine; + //Create the test runner + runner := TDUnitX.CreateRunner; + //Tell the runner to use RTTI to find Fixtures + runner.UseRTTI := True; + //tell the runner how we will log things + //Log to the console window + logger := TDUnitXConsoleLogger.Create(true); + runner.AddLogger(logger); + //Generate an NUnit compatible XML File + nunitLogger := TDUnitXXMLNUnitFileLogger.Create(TDUnitX.Options.XMLOutputFile); + runner.AddLogger(nunitLogger); + runner.FailsOnNoAsserts := False; //When true, Assertions must be made during tests; + + //Run tests + results := runner.Execute; + if not results.AllPassed then + System.ExitCode := EXIT_ERRORS; + + {$IFNDEF CI} + //We don't want this happening when running under CI. + if TDUnitX.Options.ExitBehavior = TDUnitXExitBehavior.Pause then + begin + System.Write('Done.. press key to quit.'); + System.Readln; + end; + {$ENDIF} + except + on E: Exception do + System.Writeln(E.ClassName, ': ', E.Message); + end; +end. diff --git a/exercises/resistor-color/uResistorColorTests.pas b/exercises/resistor-color/uResistorColorTests.pas new file mode 100644 index 00000000..64422c25 --- /dev/null +++ b/exercises/resistor-color/uResistorColorTests.pas @@ -0,0 +1,81 @@ +unit uResistorColorTests; + +interface +uses + DUnitX.TestFramework, uResistor; + +type + + [TestFixture('Color Codes')] + TTestResistorColor = class(TObject) + public + [Test] +// [Ignore('Comment the "[Ignore]" statement to run the test')] + procedure Black; + + [Test] + [Ignore] + procedure White; + + [Test] + [Ignore] + procedure Orange; + end; + + [TestFixture('Colors')] + TTestColors = class(TObject) + private + procedure CompareDynamicArrays(aExpected, aActual: TArray); + public + [Test] + [Ignore] + procedure Colors; + end; + +implementation + +{ TTestResistorColor } + +procedure TTestResistorColor.Black; +begin + Assert.AreEqual(0, TResistor.colorCode(rcBlack)); +end; + +procedure TTestResistorColor.White; +begin + Assert.AreEqual(9, TResistor.colorCode(rcWhite)); +end; + +procedure TTestResistorColor.Orange; +begin + Assert.AreEqual(3, TResistor.colorCode(rcOrange)); +end; + +{ TTestColors } + +procedure TTestColors.Colors; +var + Expected: TArray; + Actual: TArray; +begin + Expected := [rcBlack, rcBrown, rcRed, rcOrange, rcYellow, rcGreen, rcBlue, + rcViolet, rcGrey, rcWhite]; + + Actual := TResistor.colors; + + CompareDynamicArrays(Expected, Actual); +end; + +procedure TTestColors.CompareDynamicArrays(aExpected, aActual: TArray); +var + i: integer; +begin + Assert.AreEqual(length(aExpected), length(aActual)); + for i := Low(aExpected) to High(aExpected) do + Assert.AreEqual(aExpected[i], aActual[i]); +end; + +initialization + TDUnitX.RegisterTestFixture(TTestResistorColor); + TDUnitX.RegisterTestFixture(TTestColors); +end. diff --git a/exercises/resistor-color/uResistorExample.pas b/exercises/resistor-color/uResistorExample.pas new file mode 100644 index 00000000..2975d90d --- /dev/null +++ b/exercises/resistor-color/uResistorExample.pas @@ -0,0 +1,31 @@ +unit uResistor; + +interface + +type + TResistorColors = (rcBlack, rcBrown, rcRed, rcOrange, rcYellow, rcGreen, + rcBlue, rcViolet, rcGrey, rcWhite); + + TResistor = class + class function colorCode(aColor: TResistorColors): integer; + class function colors: TArray; + end; + + +implementation + +{ TResistor } + +class function TResistor.colorCode(aColor: TResistorColors): integer; +begin + result := ord(aColor); +end; + +class function TResistor.colors: TArray; +begin + SetLength(Result, ord(high(TResistorColors)) + 1); + for var aColor := Low(TResistorColors) to High(TResistorColors) do + Result[ord(aColor)] := aColor; +end; + +end.