Merge branch 'master' of ssh://git.bullercodeworks.com:2200/brian/exercism
This commit is contained in:
commit
5c8e8cba21
52
d/hello-world/README.md
Normal file
52
d/hello-world/README.md
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
# Hello World
|
||||||
|
|
||||||
|
The classical introductory exercise. Just say "Hello, World!".
|
||||||
|
|
||||||
|
["Hello, World!"](http://en.wikipedia.org/wiki/%22Hello,_world!%22_program) is
|
||||||
|
the traditional first program for beginning programming in a new language
|
||||||
|
or environment.
|
||||||
|
|
||||||
|
The objectives are simple:
|
||||||
|
|
||||||
|
- Write a function that returns the string "Hello, World!".
|
||||||
|
- Run the test suite and make sure that it succeeds.
|
||||||
|
- Submit your solution and check it at the website.
|
||||||
|
|
||||||
|
If everything goes well, you will be ready to fetch your first real exercise.
|
||||||
|
|
||||||
|
## Getting Started
|
||||||
|
|
||||||
|
Make sure you have read [D page](http://exercism.io/languages/dlang) on
|
||||||
|
exercism.io. This covers the basic information on setting up the development
|
||||||
|
environment expected by the exercises.
|
||||||
|
|
||||||
|
## Passing the Tests
|
||||||
|
|
||||||
|
Get the first test compiling, linking and passing by following the [three
|
||||||
|
rules of test-driven development](http://butunclebob.com/ArticleS.UncleBob.TheThreeRulesOfTdd).
|
||||||
|
Create just enough structure by declaring namespaces, functions, classes,
|
||||||
|
etc., to satisfy any compiler errors and get the test to fail. Then write
|
||||||
|
just enough code to get the test to pass. Once you've done that,
|
||||||
|
uncomment the next test by moving the following line past the next test.
|
||||||
|
|
||||||
|
```D
|
||||||
|
static if (all_tests_enabled)
|
||||||
|
```
|
||||||
|
|
||||||
|
This may result in compile errors as new constructs may be invoked that
|
||||||
|
you haven't yet declared or defined. Again, fix the compile errors minimally
|
||||||
|
to get a failing test, then change the code minimally to pass the test,
|
||||||
|
refactor your implementation for readability and expressiveness and then
|
||||||
|
go on to the next test.
|
||||||
|
|
||||||
|
Try to use standard D facilities in preference to writing your own
|
||||||
|
low-level algorithms or facilities by hand. [DRefLanguage](https://dlang.org/spec/spec.html)
|
||||||
|
and [DReference](https://dlang.org/phobos/index.html) are references to the D language and D standard library.
|
||||||
|
|
||||||
|
|
||||||
|
## Source
|
||||||
|
|
||||||
|
This is an exercise to introduce users to using Exercism [http://en.wikipedia.org/wiki/%22Hello,_world!%22_program](http://en.wikipedia.org/wiki/%22Hello,_world!%22_program)
|
||||||
|
|
||||||
|
## Submitting Incomplete Solutions
|
||||||
|
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
|
2
d/hello-world/dub.sdl
Normal file
2
d/hello-world/dub.sdl
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
name "hello-world"
|
||||||
|
buildRequirements "disallowDeprecations"
|
13
d/hello-world/source/hello_world.d
Normal file
13
d/hello-world/source/hello_world.d
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
module helloworld_test;
|
||||||
|
|
||||||
|
unittest {
|
||||||
|
const int allTestsEnabled = 0;
|
||||||
|
|
||||||
|
assert(hello() == "Hello, World!");
|
||||||
|
static if (allTestsEnabled) {
|
||||||
|
assert(hello("Alice") == "Hello, Alice!");
|
||||||
|
assert(hello("Bob") == "Hello, Bob!");
|
||||||
|
assert(hello("") == "Hello, !");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
60
delphi/hello-world/HelloWorld.dpr
Normal file
60
delphi/hello-world/HelloWorld.dpr
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
program HelloWorld;
|
||||||
|
|
||||||
|
{$IFNDEF TESTINSIGHT}
|
||||||
|
{$APPTYPE CONSOLE}
|
||||||
|
{$ENDIF}{$STRONGLINKTYPES ON}
|
||||||
|
uses
|
||||||
|
System.SysUtils,
|
||||||
|
{$IFDEF TESTINSIGHT}
|
||||||
|
TestInsight.DUnitX,
|
||||||
|
{$ENDIF }
|
||||||
|
DUnitX.Loggers.Console,
|
||||||
|
DUnitX.Loggers.Xml.NUnit,
|
||||||
|
DUnitX.TestFramework,
|
||||||
|
uTestHelloWorld in 'uTestHelloWorld.pas',
|
||||||
|
uHelloWorld in 'uHelloWorld.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 <Enter> key to quit.');
|
||||||
|
System.Readln;
|
||||||
|
end;
|
||||||
|
{$ENDIF}
|
||||||
|
except
|
||||||
|
on E: Exception do
|
||||||
|
System.Writeln(E.ClassName, ': ', E.Message);
|
||||||
|
end;
|
||||||
|
end.
|
42
delphi/hello-world/README.md
Normal file
42
delphi/hello-world/README.md
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
# Hello World
|
||||||
|
|
||||||
|
The classical introductory exercise. Just say "Hello, World!".
|
||||||
|
|
||||||
|
["Hello, World!"](http://en.wikipedia.org/wiki/%22Hello,_world!%22_program) is
|
||||||
|
the traditional first program for beginning programming in a new language
|
||||||
|
or environment.
|
||||||
|
|
||||||
|
The objectives are simple:
|
||||||
|
|
||||||
|
- Write a function that returns the string "Hello, World!".
|
||||||
|
- Run the test suite and make sure that it succeeds.
|
||||||
|
- Submit your solution and check it at the website.
|
||||||
|
|
||||||
|
If everything goes well, you will be ready to fetch your first real exercise.
|
||||||
|
|
||||||
|
## 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/installing) 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/<exerciseName>` directory.
|
||||||
|
|
||||||
|
For example, if you're submitting `ubob.pas` for the Bob exercise, the submit command would be something like `exercism submit <path_to_exercism_dir>/delphi/bob/ubob.pas`.
|
||||||
|
|
||||||
|
## Source
|
||||||
|
|
||||||
|
This is an exercise to introduce users to using Exercism [http://en.wikipedia.org/wiki/%22Hello,_world!%22_program](http://en.wikipedia.org/wiki/%22Hello,_world!%22_program)
|
||||||
|
|
||||||
|
## Submitting Incomplete Solutions
|
||||||
|
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
|
85
delphi/hello-world/uTestHelloWorld.pas
Normal file
85
delphi/hello-world/uTestHelloWorld.pas
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
(******************************************************************************
|
||||||
|
You got an error, which is exactly as it should be.
|
||||||
|
This is the first step in the Test-Driven Development
|
||||||
|
(TDD) process.
|
||||||
|
|
||||||
|
The most important part of the error is
|
||||||
|
|
||||||
|
"cannot compile"
|
||||||
|
|
||||||
|
It's looking for a file named uHelloWorld.pas that doesn't exist.
|
||||||
|
|
||||||
|
To fix the error, create a unit file named uHelloWorld.pas
|
||||||
|
in the same directory as the file uTestHelloWorld.pas.
|
||||||
|
|
||||||
|
The beginning of the new unit file should contain a unit statement:
|
||||||
|
|
||||||
|
unit uHelloWorld;
|
||||||
|
|
||||||
|
The new unit should contain Interface, Implementation, and End. statements.
|
||||||
|
|
||||||
|
The primary focus of this exercise is to provide you with a very simple
|
||||||
|
exercise that you can use to test the tools necessary for this language track,
|
||||||
|
are working correctly. To that end we are providing you with code that you may
|
||||||
|
use as the solution to this exercise:
|
||||||
|
|
||||||
|
{------------------< start solution >------------------}
|
||||||
|
unit uHelloWorld;
|
||||||
|
|
||||||
|
interface
|
||||||
|
|
||||||
|
function Hello: string;
|
||||||
|
|
||||||
|
implementation
|
||||||
|
|
||||||
|
function Hello: string;
|
||||||
|
begin
|
||||||
|
result := 'Hello, World!';
|
||||||
|
end;
|
||||||
|
|
||||||
|
end.
|
||||||
|
{------------------< end solution >------------------}
|
||||||
|
|
||||||
|
Hint: Delphi will take care of all this if you instruct it to add a new unit
|
||||||
|
to your project. Be sure to save the new unit as uHelloWorld.pas before
|
||||||
|
trying to compile again.
|
||||||
|
|
||||||
|
For more guidance as you work on this exercise, see
|
||||||
|
GETTING_STARTED.md.
|
||||||
|
******************************************************************************)
|
||||||
|
unit uTestHelloWorld;
|
||||||
|
|
||||||
|
interface
|
||||||
|
uses
|
||||||
|
DUnitX.TestFramework;
|
||||||
|
|
||||||
|
type
|
||||||
|
[TestFixture]
|
||||||
|
HelloWorldTest = class(TObject)
|
||||||
|
public
|
||||||
|
[Test]
|
||||||
|
procedure Say_hi;
|
||||||
|
end;
|
||||||
|
|
||||||
|
implementation
|
||||||
|
uses uHelloWorld;
|
||||||
|
|
||||||
|
procedure HelloWorldTest.Say_hi;
|
||||||
|
var
|
||||||
|
Expected: string;
|
||||||
|
Actual: string;
|
||||||
|
begin
|
||||||
|
Expected := 'Hello, World!'; //Expected: This is what is expected to be returned by the function/method (Hello)
|
||||||
|
Actual := Hello; //Actual: This is what is actually returned by the function/method (Hello)
|
||||||
|
Assert.AreEqual(Expected, Actual);
|
||||||
|
|
||||||
|
//As you progress in this track you will find that not every exercise has Expected and
|
||||||
|
//Actual defined as explicitly as they have been above. Often times you may find
|
||||||
|
//that the Expected outcome is inserted as an inline statement and the the call
|
||||||
|
//to the method being tested will be inserted in the Actual position of AreEqual like so:
|
||||||
|
//Assert.AreEqual('Hello, World!', Hello);
|
||||||
|
end;
|
||||||
|
|
||||||
|
initialization
|
||||||
|
TDUnitX.RegisterTestFixture(HelloWorldTest);
|
||||||
|
end.
|
53
go/accumulate/README.md
Normal file
53
go/accumulate/README.md
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
# Accumulate
|
||||||
|
|
||||||
|
Implement the `accumulate` operation, which, given a collection and an
|
||||||
|
operation to perform on each element of the collection, returns a new
|
||||||
|
collection containing the result of applying that operation to each element of
|
||||||
|
the input collection.
|
||||||
|
|
||||||
|
Given the collection of numbers:
|
||||||
|
|
||||||
|
- 1, 2, 3, 4, 5
|
||||||
|
|
||||||
|
And the operation:
|
||||||
|
|
||||||
|
- square a number (`x => x * x`)
|
||||||
|
|
||||||
|
Your code should be able to produce the collection of squares:
|
||||||
|
|
||||||
|
- 1, 4, 9, 16, 25
|
||||||
|
|
||||||
|
Check out the test suite to see the expected function signature.
|
||||||
|
|
||||||
|
## Restrictions
|
||||||
|
|
||||||
|
Keep your hands off that collect/map/fmap/whatchamacallit functionality
|
||||||
|
provided by your standard library!
|
||||||
|
Solve this one yourself using other basic tools instead.
|
||||||
|
|
||||||
|
Lisp specific: it's perfectly fine to use `MAPCAR` or the equivalent,
|
||||||
|
as this is idiomatic Lisp, not a library function.
|
||||||
|
|
||||||
|
## Running the tests
|
||||||
|
|
||||||
|
To run the tests run the command `go test` from within the exercise directory.
|
||||||
|
|
||||||
|
If the test suite contains benchmarks, you can run these with the `-bench`
|
||||||
|
flag:
|
||||||
|
|
||||||
|
go test -bench .
|
||||||
|
|
||||||
|
Keep in mind that each reviewer will run benchmarks on a different machine, with
|
||||||
|
different specs, so the results from these benchmark tests may vary.
|
||||||
|
|
||||||
|
## Further information
|
||||||
|
|
||||||
|
For more detailed information about the Go track, including how to get help if
|
||||||
|
you're having trouble, please visit the exercism.io [Go language page](http://exercism.io/languages/go/about).
|
||||||
|
|
||||||
|
## Source
|
||||||
|
|
||||||
|
Conversation with James Edward Gray II [https://twitter.com/jeg2](https://twitter.com/jeg2)
|
||||||
|
|
||||||
|
## Submitting Incomplete Solutions
|
||||||
|
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
|
11
go/accumulate/accumulate.go
Normal file
11
go/accumulate/accumulate.go
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
package accumulate
|
||||||
|
|
||||||
|
const testVersion = 1
|
||||||
|
|
||||||
|
func Accumulate(vals []string, f func(string) string) []string {
|
||||||
|
ret := make([]string, len(vals))
|
||||||
|
for i := range vals {
|
||||||
|
ret[i] = f(vals[i])
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
57
go/accumulate/accumulate_test.go
Normal file
57
go/accumulate/accumulate_test.go
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
package accumulate
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
const targetTestVersion = 1
|
||||||
|
|
||||||
|
func echo(c string) string {
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
func capitalize(word string) string {
|
||||||
|
return strings.Title(word)
|
||||||
|
}
|
||||||
|
|
||||||
|
var tests = []struct {
|
||||||
|
expected []string
|
||||||
|
given []string
|
||||||
|
converter func(string) string
|
||||||
|
description string
|
||||||
|
}{
|
||||||
|
{[]string{}, []string{}, echo, "echo"},
|
||||||
|
{[]string{"echo", "echo", "echo", "echo"}, []string{"echo", "echo", "echo", "echo"}, echo, "echo"},
|
||||||
|
{[]string{"First", "Letter", "Only"}, []string{"first", "letter", "only"}, capitalize, "capitalize"},
|
||||||
|
{[]string{"HELLO", "WORLD"}, []string{"hello", "world"}, strings.ToUpper, "strings.ToUpper"},
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTestVersion(t *testing.T) {
|
||||||
|
if testVersion != targetTestVersion {
|
||||||
|
t.Fatalf("Found testVersion = %v, want %v", testVersion, targetTestVersion)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAccumulate(t *testing.T) {
|
||||||
|
for _, test := range tests {
|
||||||
|
actual := Accumulate(test.given, test.converter)
|
||||||
|
if fmt.Sprintf("%q", actual) != fmt.Sprintf("%q", test.expected) {
|
||||||
|
t.Fatalf("Accumulate(%q, %q): expected %q, actual %q", test.given, test.description, test.expected, actual)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkAccumulate(b *testing.B) {
|
||||||
|
b.StopTimer()
|
||||||
|
for _, test := range tests {
|
||||||
|
b.StartTimer()
|
||||||
|
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
Accumulate(test.given, test.converter)
|
||||||
|
}
|
||||||
|
|
||||||
|
b.StopTimer()
|
||||||
|
}
|
||||||
|
}
|
31
go/error-handling/README.md
Normal file
31
go/error-handling/README.md
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
# Error Handling
|
||||||
|
|
||||||
|
Implement various kinds of error handling and resource management.
|
||||||
|
|
||||||
|
An important point of programming is how to handle errors and close
|
||||||
|
resources even if errors occur.
|
||||||
|
|
||||||
|
This exercise requires you to handle various errors. Because error handling
|
||||||
|
is rather programming language specific you'll have to refer to the tests
|
||||||
|
for your track to see what's exactly required.
|
||||||
|
|
||||||
|
## Running the tests
|
||||||
|
|
||||||
|
To run the tests run the command `go test` from within the exercise directory.
|
||||||
|
|
||||||
|
If the test suite contains benchmarks, you can run these with the `-bench`
|
||||||
|
flag:
|
||||||
|
|
||||||
|
go test -bench .
|
||||||
|
|
||||||
|
Keep in mind that each reviewer will run benchmarks on a different machine, with
|
||||||
|
different specs, so the results from these benchmark tests may vary.
|
||||||
|
|
||||||
|
## Further information
|
||||||
|
|
||||||
|
For more detailed information about the Go track, including how to get help if
|
||||||
|
you're having trouble, please visit the exercism.io [Go language page](http://exercism.io/languages/go/about).
|
||||||
|
|
||||||
|
|
||||||
|
## Submitting Incomplete Solutions
|
||||||
|
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
|
57
go/error-handling/common.go
Normal file
57
go/error-handling/common.go
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
package erratum
|
||||||
|
|
||||||
|
import "io"
|
||||||
|
|
||||||
|
// These are the support types and interface definitions used in the
|
||||||
|
// implementation if your Use function. See the test suite file at
|
||||||
|
// for information on the expected implementation.
|
||||||
|
//
|
||||||
|
// Because this is part of the package "erratum", if your solution file
|
||||||
|
// is also declared in the package you will automatically have access to
|
||||||
|
// these definitions (you do not have to re-declare them).
|
||||||
|
|
||||||
|
// TransientError is an error that may occur while opening a resource via
|
||||||
|
// ResourceOpener.
|
||||||
|
type TransientError struct {
|
||||||
|
err error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e TransientError) Error() string {
|
||||||
|
return e.err.Error()
|
||||||
|
}
|
||||||
|
|
||||||
|
// FrobError is a possible error from doing some frobbing, your implementation
|
||||||
|
// will require calling your Resource's Defrob(string) method.
|
||||||
|
// When this error occurs, the FrobError's defrobTag string will contain the
|
||||||
|
// string you must pass into Defrob.
|
||||||
|
type FrobError struct {
|
||||||
|
defrobTag string
|
||||||
|
inner error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e FrobError) Error() string {
|
||||||
|
return e.inner.Error()
|
||||||
|
}
|
||||||
|
|
||||||
|
type Resource interface {
|
||||||
|
|
||||||
|
// Resource is using composition to inherit the requirements of the io.Closer
|
||||||
|
// interface. What this means is that a Resource will have a .Close() method.
|
||||||
|
io.Closer
|
||||||
|
|
||||||
|
// Frob does something with the input string.
|
||||||
|
// Because this is an incredibly badly designed system if there is an error
|
||||||
|
// it panics.
|
||||||
|
//
|
||||||
|
// The paniced error may be a FrobError in which case Defrob should be called
|
||||||
|
// with the defrobTag string.
|
||||||
|
Frob(string)
|
||||||
|
|
||||||
|
Defrob(string)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ResourceOpener is a function that creates a resource.
|
||||||
|
//
|
||||||
|
// It may return a wrapped error of type TransientError. In this case the resource
|
||||||
|
// is temporarily unavailable and the caller should retry soon.
|
||||||
|
type ResourceOpener func() (Resource, error)
|
35
go/error-handling/error_handling.go
Normal file
35
go/error-handling/error_handling.go
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
package erratum
|
||||||
|
|
||||||
|
const testVersion = 2
|
||||||
|
|
||||||
|
func Use(o ResourceOpener, inp string) (err error) {
|
||||||
|
var r Resource
|
||||||
|
moveOn := false
|
||||||
|
for !moveOn {
|
||||||
|
moveOn = true
|
||||||
|
if r, err = o(); err != nil {
|
||||||
|
switch err.(type) {
|
||||||
|
case TransientError:
|
||||||
|
moveOn = false
|
||||||
|
default:
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
defer r.Close()
|
||||||
|
defer func() {
|
||||||
|
if rec := recover(); rec != nil {
|
||||||
|
switch v := rec.(type) {
|
||||||
|
case FrobError:
|
||||||
|
r.Defrob(v.defrobTag)
|
||||||
|
err = v
|
||||||
|
case error:
|
||||||
|
err = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
r.Frob(inp)
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
191
go/error-handling/error_handling_test.go
Normal file
191
go/error-handling/error_handling_test.go
Normal file
@ -0,0 +1,191 @@
|
|||||||
|
package erratum
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Because this exercise is generally unique to each language and how it
|
||||||
|
// handles errors, most of the definition of your expected solution is provided
|
||||||
|
// here instead of the README.
|
||||||
|
// You should read this carefully (more than once) before implementation.
|
||||||
|
|
||||||
|
// Define a function `Use(o ResourceOpener, input string) error` that opens a
|
||||||
|
// resource, calls Frob(input) and closes the resource (in all cases). Your
|
||||||
|
// function should properly handle errors, as defined by the expectations of
|
||||||
|
// this test suite. ResourceOpener will be a function you may invoke directly
|
||||||
|
// `o()` in an attempt to "open" the resource. It returns a Resource and error
|
||||||
|
// value in the idiomatic Go fashion:
|
||||||
|
// https://blog.golang.org/error-handling-and-go
|
||||||
|
//
|
||||||
|
// See the ./common.go file for the definitions of Resource, ResourceOpener,
|
||||||
|
// FrobError and TransientError.
|
||||||
|
//
|
||||||
|
// There will be a few places in your Use function where errors may occur:
|
||||||
|
//
|
||||||
|
// - Invoking the ResourceOpener function passed into Use as the first
|
||||||
|
// parameter, it may fail with a TransientError, if so keep trying to open it.
|
||||||
|
// If it is some other sort of error, return it.
|
||||||
|
//
|
||||||
|
// - Calling the Frob function on the Resource returned from the ResourceOpener
|
||||||
|
// function, it may panic with a FrobError (or another type of error). If
|
||||||
|
// it is indeed a FrobError you will have to call the Resource's Defrob
|
||||||
|
// function using the FrobError's defrobTag variable as input. Either way
|
||||||
|
// return the error.
|
||||||
|
//
|
||||||
|
// Also note: if the Resource was opened successfully make sure to call its
|
||||||
|
// Close function no matter what (even if errors occur).
|
||||||
|
//
|
||||||
|
// If you are new to Go errors or panics here is a good place to start:
|
||||||
|
// https://blog.golang.org/defer-panic-and-recover
|
||||||
|
//
|
||||||
|
// You may also need to look at named return values as a helpful way to
|
||||||
|
// return error information from panic recovery:
|
||||||
|
// https://tour.golang.org/basics/7
|
||||||
|
|
||||||
|
const targetTestVersion = 2
|
||||||
|
|
||||||
|
// Little helper to let us customize behaviour of the resource on a per-test
|
||||||
|
// basis.
|
||||||
|
type mockResource struct {
|
||||||
|
close func() error
|
||||||
|
frob func(string)
|
||||||
|
defrob func(string)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mr mockResource) Close() error { return mr.close() }
|
||||||
|
func (mr mockResource) Frob(input string) { mr.frob(input) }
|
||||||
|
func (mr mockResource) Defrob(tag string) { mr.defrob(tag) }
|
||||||
|
|
||||||
|
func TestTestVersion(t *testing.T) {
|
||||||
|
if testVersion != targetTestVersion {
|
||||||
|
t.Fatalf("Found testVersion = %v, want %v", testVersion, targetTestVersion)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use should not return an error on the "happy" path.
|
||||||
|
func TestNoErrors(t *testing.T) {
|
||||||
|
var frobInput string
|
||||||
|
var closeCalled bool
|
||||||
|
mr := mockResource{
|
||||||
|
close: func() error { closeCalled = true; return nil },
|
||||||
|
frob: func(input string) { frobInput = input },
|
||||||
|
}
|
||||||
|
opener := func() (Resource, error) { return mr, nil }
|
||||||
|
inp := "hello"
|
||||||
|
err := Use(opener, inp)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Unexpected error from Use: %v", err)
|
||||||
|
}
|
||||||
|
if frobInput != inp {
|
||||||
|
t.Fatalf("Wrong string passed to Frob: got %v, expected %v", frobInput, inp)
|
||||||
|
}
|
||||||
|
if !closeCalled {
|
||||||
|
t.Fatalf("Close was not called")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use should keep trying if a transient error is returned on open.
|
||||||
|
func TestKeepTryOpenOnTransient(t *testing.T) {
|
||||||
|
var frobInput string
|
||||||
|
mr := mockResource{
|
||||||
|
close: func() error { return nil },
|
||||||
|
frob: func(input string) { frobInput = input },
|
||||||
|
}
|
||||||
|
nthCall := 0
|
||||||
|
opener := func() (Resource, error) {
|
||||||
|
if nthCall < 3 {
|
||||||
|
nthCall++
|
||||||
|
return mockResource{}, TransientError{errors.New("some error")}
|
||||||
|
}
|
||||||
|
return mr, nil
|
||||||
|
}
|
||||||
|
inp := "hello"
|
||||||
|
err := Use(opener, inp)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Unexpected error from Use: %v", err)
|
||||||
|
}
|
||||||
|
if frobInput != inp {
|
||||||
|
t.Fatalf("Wrong string passed to Frob: got %v, expected %v", frobInput, inp)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use should fail if a non-transient error is returned on open.
|
||||||
|
func TestFailOpenOnNonTransient(t *testing.T) {
|
||||||
|
nthCall := 0
|
||||||
|
opener := func() (Resource, error) {
|
||||||
|
if nthCall < 3 {
|
||||||
|
nthCall++
|
||||||
|
return mockResource{}, TransientError{errors.New("some error")}
|
||||||
|
}
|
||||||
|
return nil, errors.New("too awesome")
|
||||||
|
}
|
||||||
|
inp := "hello"
|
||||||
|
err := Use(opener, inp)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("Unexpected lack of error from Use")
|
||||||
|
}
|
||||||
|
if err.Error() != "too awesome" {
|
||||||
|
t.Fatalf("Invalid error returned from Use")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use should call Defrob and Close on FrobError panic from Frob
|
||||||
|
// and return the error.
|
||||||
|
func TestCallDefrobAndCloseOnFrobError(t *testing.T) {
|
||||||
|
tag := "moo"
|
||||||
|
var closeCalled bool
|
||||||
|
var defrobTag string
|
||||||
|
mr := mockResource{
|
||||||
|
close: func() error { closeCalled = true; return nil },
|
||||||
|
frob: func(input string) { panic(FrobError{tag, errors.New("meh")}) },
|
||||||
|
defrob: func(tag string) {
|
||||||
|
if closeCalled {
|
||||||
|
t.Fatalf("Close was called before Defrob")
|
||||||
|
}
|
||||||
|
defrobTag = tag
|
||||||
|
},
|
||||||
|
}
|
||||||
|
opener := func() (Resource, error) { return mr, nil }
|
||||||
|
inp := "hello"
|
||||||
|
err := Use(opener, inp)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("Unexpected lack of error from Use")
|
||||||
|
}
|
||||||
|
if err.Error() != "meh" {
|
||||||
|
t.Fatalf("Invalid error returned from Use")
|
||||||
|
}
|
||||||
|
if defrobTag != tag {
|
||||||
|
t.Fatalf("Wrong string passed to Defrob: got %v, expected %v", defrobTag, tag)
|
||||||
|
}
|
||||||
|
if !closeCalled {
|
||||||
|
t.Fatalf("Close was not called")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use should call Close but not Defrob on non-FrobError panic from Frob
|
||||||
|
// and return the error.
|
||||||
|
func TestCallCloseNonOnFrobError(t *testing.T) {
|
||||||
|
var closeCalled bool
|
||||||
|
var defrobCalled bool
|
||||||
|
mr := mockResource{
|
||||||
|
close: func() error { closeCalled = true; return nil },
|
||||||
|
frob: func(input string) { panic(errors.New("meh")) },
|
||||||
|
defrob: func(tag string) { defrobCalled = true },
|
||||||
|
}
|
||||||
|
opener := func() (Resource, error) { return mr, nil }
|
||||||
|
inp := "hello"
|
||||||
|
err := Use(opener, inp)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("Unexpected lack of error from Use")
|
||||||
|
}
|
||||||
|
if err.Error() != "meh" {
|
||||||
|
t.Fatalf("Invalid error returned from Use")
|
||||||
|
}
|
||||||
|
if defrobCalled {
|
||||||
|
t.Fatalf("Defrob was called")
|
||||||
|
}
|
||||||
|
if !closeCalled {
|
||||||
|
t.Fatalf("Close was not called")
|
||||||
|
}
|
||||||
|
}
|
@ -1,26 +1,109 @@
|
|||||||
package paasio
|
package paasio
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
const testVersion = 3
|
const testVersion = 3
|
||||||
|
|
||||||
func NewWriteCounter() WriteCounter {
|
func NewReadCounter(r io.Reader) ReadCounter {
|
||||||
w := new(WriteCounter)
|
return &readCounter{
|
||||||
return w
|
r: r,
|
||||||
|
lock: new(sync.Mutex),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WriteCounter) WriteCount() (int64, int) {
|
type readCounter struct {
|
||||||
|
r io.Reader
|
||||||
|
bytesRead int64
|
||||||
|
ops int
|
||||||
|
lock *sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rc *readCounter) Read(p []byte) (int, error) {
|
||||||
|
m, err := rc.r.Read(p)
|
||||||
|
rc.lock.Lock()
|
||||||
|
rc.bytesRead += int64(m)
|
||||||
|
rc.ops++
|
||||||
|
rc.lock.Unlock()
|
||||||
|
return m, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rc *readCounter) ReadCount() (n int64, ops int) {
|
||||||
|
rc.lock.Lock()
|
||||||
|
n, ops = rc.bytesRead, rc.ops
|
||||||
|
rc.lock.Unlock()
|
||||||
|
return n, ops
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewWriteCounter(w io.Writer) WriteCounter {
|
||||||
|
return &writeCounter{
|
||||||
|
w: w,
|
||||||
|
lock: new(sync.Mutex),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type writeCounter struct {
|
||||||
|
w io.Writer
|
||||||
|
bytesWrote int64
|
||||||
|
ops int
|
||||||
|
lock *sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
func (wc *writeCounter) Write(p []byte) (int, error) {
|
||||||
|
m, err := wc.w.Write(p)
|
||||||
|
wc.lock.Lock()
|
||||||
|
wc.bytesWrote += int64(m)
|
||||||
|
wc.ops++
|
||||||
|
wc.lock.Unlock()
|
||||||
|
return m, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (wc *writeCounter) WriteCount() (n int64, ops int) {
|
||||||
|
wc.lock.Lock()
|
||||||
|
n, ops = wc.bytesWrote, wc.ops
|
||||||
|
wc.lock.Unlock()
|
||||||
|
return n, ops
|
||||||
|
}
|
||||||
|
|
||||||
|
type ReadWriter interface {
|
||||||
|
io.Reader
|
||||||
|
io.Writer
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewReadWriteCounter(rw ReadWriter) ReadWriteCounter {
|
||||||
|
return &readWriteCounter{
|
||||||
|
r: NewReadCounter(rw),
|
||||||
|
w: NewWriteCounter(rw),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type readWriteCounter struct {
|
||||||
|
r ReadCounter
|
||||||
|
w WriteCounter
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rw *readWriteCounter) Read(p []byte) (int, error) {
|
||||||
|
return rw.r.Read(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rw *readWriteCounter) Write(p []byte) (int, error) {
|
||||||
|
return rw.w.Write(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rw *readWriteCounter) ReadCount() (n int64, ops int) {
|
||||||
|
return rw.r.ReadCount()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rw *readWriteCounter) WriteCount() (n int64, ops int) {
|
||||||
|
return rw.w.WriteCount()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (nr *nopReader) ReadCount() (n int64, ops int) {
|
||||||
return 0, 0
|
return 0, 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewReadCounter() ReadCounter {
|
func (nw *nopWriter) WriteCount() (n int64, ops int) {
|
||||||
r := new(ReadCounter)
|
|
||||||
return r
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *ReadCounter) ReadCount() (int64, int) {
|
|
||||||
return 0, 0
|
return 0, 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewReadWriteCounter() *ReadWriteCounter {
|
|
||||||
r := new(ReadWriteCounter)
|
|
||||||
return r
|
|
||||||
}
|
|
||||||
|
BIN
go/pov/cmd/cmd
Executable file
BIN
go/pov/cmd/cmd
Executable file
Binary file not shown.
13
go/pov/cmd/main.go
Normal file
13
go/pov/cmd/main.go
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import pov ".."
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
g := pov.New()
|
||||||
|
g.AddNode("sibling")
|
||||||
|
g.AddNode("x")
|
||||||
|
g.AddNode("parent")
|
||||||
|
g.AddArc("parent", "sibling")
|
||||||
|
g.AddArc("parent", "x")
|
||||||
|
pov.PrintGraph(g)
|
||||||
|
}
|
134
go/pov/pov.go
Normal file
134
go/pov/pov.go
Normal file
@ -0,0 +1,134 @@
|
|||||||
|
package pov
|
||||||
|
|
||||||
|
const testVersion = 2
|
||||||
|
|
||||||
|
type Graph struct {
|
||||||
|
leaves []Node
|
||||||
|
}
|
||||||
|
|
||||||
|
func New() *Graph {
|
||||||
|
g := new(Graph)
|
||||||
|
return g
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetNode returns the node from the graph with label lbl
|
||||||
|
func (g *Graph) GetNode(lbl string) *Node {
|
||||||
|
for i := range g.leaves {
|
||||||
|
if n := g.leaves[i].GetNode(lbl); n != nil {
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddNode adds a top level leaf with label lbl
|
||||||
|
func (g *Graph) AddNode(lbl string) {
|
||||||
|
g.leaves = append(g.leaves, Node{label: lbl})
|
||||||
|
}
|
||||||
|
|
||||||
|
// addRealNode adds the node n to the top level
|
||||||
|
func (g *Graph) addRealNode(n *Node) {
|
||||||
|
g.leaves = append(g.leaves, *n)
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddArc creates a new node after to named from
|
||||||
|
func (g *Graph) AddArc(from, to string) {
|
||||||
|
if n := g.GetNode(to); n != nil {
|
||||||
|
n.AddNode(from)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ArcList returns a list of all arcs
|
||||||
|
func (g *Graph) ArcList() []string {
|
||||||
|
var ret []string
|
||||||
|
for i := range g.leaves {
|
||||||
|
ret = append(ret, g.leaves[i].ArcList()...)
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
// ChangeRoot changes the graph from starting at oldRoot going to newRoot
|
||||||
|
func (g *Graph) ChangeRoot(oldRoot, newRoot string) *Graph {
|
||||||
|
// First of all, find the newRoot node
|
||||||
|
ret := New()
|
||||||
|
// The new graph will start with newRoot and have newRoot's leaves
|
||||||
|
var rt *Node
|
||||||
|
if rt = g.GetNode(newRoot); rt == nil {
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
// It'll have one more leaf, it's parent node
|
||||||
|
//rt.addRealNode(g.GetNode(oldRoot))
|
||||||
|
ret.addRealNode(rt)
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *Graph) getPath(from, to string) []string {
|
||||||
|
var ret []string
|
||||||
|
// Get the 'from' node
|
||||||
|
frNode := g.GetNode(from)
|
||||||
|
if frNode == nil {
|
||||||
|
// Couldn't find the starting node
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
// Just in case we got the same value for both
|
||||||
|
if from == to {
|
||||||
|
return []string{from}
|
||||||
|
}
|
||||||
|
// Found it
|
||||||
|
return frNode.getPath(to)
|
||||||
|
}
|
||||||
|
|
||||||
|
type Node struct {
|
||||||
|
label string
|
||||||
|
leaves []Node
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *Node) AddNode(lbl string) {
|
||||||
|
n.leaves = append(n.leaves, Node{label: lbl})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *Node) addRealNode(nd *Node) {
|
||||||
|
n.leaves = append(n.leaves, *nd)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *Node) GetNode(lbl string) *Node {
|
||||||
|
if n.label == lbl {
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
for i := range n.leaves {
|
||||||
|
if r := n.leaves[i].GetNode(lbl); r != nil {
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *Node) ArcList() []string {
|
||||||
|
var ret []string
|
||||||
|
for i := range n.leaves {
|
||||||
|
ret = append(ret, n.leaves[i].label+" -> "+n.label)
|
||||||
|
ret = append(ret, n.leaves[i].ArcList()...)
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *Node) getPath(to string) []string {
|
||||||
|
ret := []string{n.label}
|
||||||
|
if n.label == to {
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
var i int
|
||||||
|
var found bool
|
||||||
|
for i = range n.leaves {
|
||||||
|
if n.leaves[i].GetNode(to) != nil {
|
||||||
|
found = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !found {
|
||||||
|
// We didn't find a path... :(
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
// n.leaves[i] should be the right leaf now
|
||||||
|
return append(ret, n.leaves[i].getPath(to)...)
|
||||||
|
}
|
20
go/pov/pov_helper.go
Normal file
20
go/pov/pov_helper.go
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package pov
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func PrintGraph(g *Graph) {
|
||||||
|
for i := range g.leaves {
|
||||||
|
PrintNode(&g.leaves[i], 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func PrintNode(n *Node, lvl int) {
|
||||||
|
strings.Repeat(" ", lvl)
|
||||||
|
fmt.Println(n.label)
|
||||||
|
for i := range n.leaves {
|
||||||
|
PrintNode(&n.leaves[i], lvl+1)
|
||||||
|
}
|
||||||
|
}
|
15
perl6/hello-world/HelloWorld.pm6
Normal file
15
perl6/hello-world/HelloWorld.pm6
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
#`( |