Skip to content

Instantly share code, notes, and snippets.

@NikolayGenov
Created November 29, 2016 00:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NikolayGenov/b1a2555b6bb7edcaaa810a9a27c4b788 to your computer and use it in GitHub Desktop.
Save NikolayGenov/b1a2555b6bb7edcaaa810a9a27c4b788 to your computer and use it in GitHub Desktop.
Tests for Homework 3
package main
import (
"fmt"
"testing"
"time"
)
type adder struct {
augend int
}
func (a adder) Execute(addend int) (int, error) {
result := a.augend + addend
if result > 127 {
return 0, fmt.Errorf("Result %d exceeds the adder threshold", a)
}
return result, nil
}
func ExamplePipeline_first() {
if res, err := Pipeline(adder{50}, adder{60}).Execute(10); err != nil {
fmt.Printf("The pipeline returned an error\n")
} else {
fmt.Printf("The pipeline returned %d\n", res)
}
// Output:
// The pipeline returned 120
}
func ExamplePipeline_second() {
p := Pipeline(adder{20}, adder{10}, adder{-50})
if res, err := p.Execute(100); err != nil {
fmt.Printf("The pipeline returned an error\n")
} else {
fmt.Printf("The pipeline returned %d\n", res)
}
// Output:
// The pipeline returned an error
}
func ExamplePipeline_empty() {
p := Pipeline()
if res, err := p.Execute(100); err != nil {
fmt.Printf("The pipeline returned an error\n")
} else {
fmt.Printf("The pipeline returned %d\n", res)
}
// Output:
// The pipeline returned an error
}
type lazyAdder struct {
adder
delay time.Duration
}
func (la lazyAdder) Execute(addend int) (int, error) {
time.Sleep(la.delay * time.Millisecond)
return la.adder.Execute(addend)
}
func TestFastest(t *testing.T) {
f := Fastest(
lazyAdder{adder{20}, 500},
lazyAdder{adder{50}, 300},
adder{41},
)
if result, err := f.Execute(1); err != nil {
t.Errorf("Received an unexpected error")
} else if result != 42 {
t.Errorf("Expected to receive 42 but received %d", result)
}
}
func ExampleFastest_empty() {
f := Fastest()
if res, err := f.Execute(1); err != nil {
fmt.Printf("Returned an exptected error")
} else if res != 42 {
fmt.Printf("The pipeline %v\n", err)
}
// Output:
// Returned an exptected error
}
func ExampleFastest_error() {
f := Fastest(
lazyAdder{adder{50}, 10},
lazyAdder{adder{30}, 2000},
)
if res, err := f.Execute(100); err != nil {
fmt.Printf("%s", err)
} else if res != 42 {
fmt.Printf("The pipeline %v\n", err)
}
// Output:
// Result {50} exceeds the adder threshold
}
func TestTimed(t *testing.T) {
_, e1 := Timed(lazyAdder{adder{20}, 50}, 2*time.Millisecond).Execute(2)
if e1 == nil {
t.Error("Expected an error in e1")
}
r2, e2 := Timed(lazyAdder{adder{20}, 50}, 300*time.Millisecond).Execute(2)
if e2 != nil {
t.Error("Did not expect an error in e2")
}
if r2 != 22 {
t.Errorf("Expected to receive 22 in r2 but received %d", r2)
}
_, e3 := Timed(lazyAdder{adder{20}, 50}, 6*time.Millisecond).Execute(2)
if e3 == nil {
t.Errorf("Expected an error in e3")
}
_, e4 := Timed(adder{127}, 300*time.Millisecond).Execute(2)
if e4 == nil {
t.Errorf("Did not expect an error in e4")
}
}
func ExampleConcurrentMapReduce() {
reduce := func(results []int) int {
smallest := 128
for _, v := range results {
if v < smallest {
smallest = v
}
}
return smallest
}
mr := ConcurrentMapReduce(reduce, adder{30}, adder{50}, adder{20})
if res, err := mr.Execute(5); err != nil {
fmt.Printf("We got an error!\n")
} else {
fmt.Printf("The ConcurrentMapReduce returned %d\n", res)
}
// Output:
// The ConcurrentMapReduce returned 25
}
func ExampleConcurrentMapReduce_withSum() {
reduce := func(results []int) int {
sum := 0
for _, v := range results {
sum += v
}
return sum
}
mr := ConcurrentMapReduce(reduce, adder{30}, adder{50}, adder{20})
if res, err := mr.Execute(5); err != nil {
fmt.Printf("We got an error!\n")
} else {
fmt.Printf("The ConcurrentMapReduce returned %d\n", res)
}
// Output:
// The ConcurrentMapReduce returned 115
}
func ExampleConcurrentMapReduce_with_error() {
reduce := func(results []int) int {
smallest := 128
for _, v := range results {
if v < smallest {
smallest = v
}
}
return smallest
}
mr := ConcurrentMapReduce(reduce, adder{130}, adder{50}, adder{20})
if res, err := mr.Execute(5); err != nil {
fmt.Printf("We got an error!: %v\n", err)
} else {
fmt.Printf("The ConcurrentMapReduce returned %d\n", res)
}
// Output:
// We got an error!: Result {130} exceeds the adder threshold
}
func ExampleConcurrentMapReduce_with_no_tasks() {
mr := ConcurrentMapReduce(func(results []int) int { return 0 })
if res, err := mr.Execute(5); err != nil {
fmt.Printf("We got an exptected error!\n")
} else {
fmt.Printf("The ConcurrentMapReduce returned %d\n", res)
}
// Output:
// We got an exptected error!
}
func TestGreatestSearcherSimple(t *testing.T) {
tasks := make(chan Task)
gs := GreatestSearcher(2, tasks) // We accept two errors
go func() {
tasks <- adder{4}
tasks <- lazyAdder{adder{22}, 20}
tasks <- adder{125} // This should be the first acceptable error
time.Sleep(50 * time.Millisecond)
tasks <- adder{32} // This should be the winner
// This should time out be the second acceptable error
tasks <- Timed(lazyAdder{adder{100}, 2000}, 20*time.Millisecond)
// If we uncomment this, the whole gs.Execute() should return an error
// tasks <- adder{127} // third unacceptable error
close(tasks)
}()
expResult := 42
result, err := gs.Execute(10)
if err != nil {
t.Errorf("Received an unexpected error %s", err)
} else if result != expResult {
t.Errorf("Received result %d when expecting %d", result, expResult)
}
}
func TestGreatestSearcherNotSoSimple(t *testing.T) {
tasks := make(chan Task)
gs := GreatestSearcher(2, tasks)
go func() {
tasks <- adder{4}
tasks <- Timed(lazyAdder{adder{110}, 500}, 20*time.Millisecond)
tasks <- adder{42}
tasks <- lazyAdder{adder{22}, 20}
tasks <- adder{125}
time.Sleep(5 * time.Millisecond)
tasks <- adder{62}
tasks <- adder{32}
tasks <- Timed(lazyAdder{adder{100}, 5}, 20*time.Millisecond)
close(tasks)
}()
expResult := 110
result, err := gs.Execute(10)
if err != nil {
t.Errorf("Received an unexpected error %s", err)
} else if result != expResult {
t.Errorf("Received result %d when expecting %d", result, expResult)
}
}
func TestGreatestSearcherWithError(t *testing.T) {
tasks := make(chan Task)
gs := GreatestSearcher(2, tasks)
go func() {
tasks <- adder{4}
tasks <- lazyAdder{adder{22}, 20}
tasks <- adder{125} // <---
time.Sleep(50 * time.Millisecond)
tasks <- adder{32}
tasks <- Timed(lazyAdder{adder{100}, 2000}, 20*time.Millisecond) // <---
tasks <- adder{127} // <---
close(tasks)
}()
//Executed 3 errors
result, err := gs.Execute(10)
if err == nil {
t.Errorf("We exptected an error here")
}
if result != 0 {
t.Errorf("Recieved non null value: %d", result)
}
}
func TestGreatestSearcherWithErrorNoTasksWereGiven(t *testing.T) {
tasks := make(chan Task)
gs := GreatestSearcher(2, tasks)
go func() {
//EMPTY SPACE
close(tasks)
}()
result, err := gs.Execute(10)
if err == nil {
t.Errorf("We exptected an error here")
}
if result != 0 {
t.Errorf("Recieved non null value: %d", result)
}
}
func TestGreatestSearcherWithErrorNoSuccessfulTasks(t *testing.T) {
tasks := make(chan Task)
gs := GreatestSearcher(2, tasks)
go func() {
tasks <- lazyAdder{adder{127}, 1}
close(tasks)
}()
result, err := gs.Execute(10)
if err == nil {
t.Errorf("We exptected an error here")
}
if result != 0 {
t.Errorf("Recieved non null value: %d", result)
}
}
func TestGreatestSearcherWithRaceErrorFailing(t *testing.T) {
//Here 100000 errors are executed, but the limit is 99999
//Trying to find races with `go test -race`
tasks := make(chan Task)
times := 100000
gs := GreatestSearcher(times-1, tasks)
go func() {
tasks <- lazyAdder{adder{10}, 1}
for i := 0; i < times; i++ {
tasks <- lazyAdder{adder{127}, 1}
}
close(tasks)
}()
result, err := gs.Execute(10)
if err == nil {
t.Errorf("We exptected an error here")
}
if result != 0 {
t.Errorf("Recieved non null value: %d", result)
}
}
func TestGreatestSearcherWithRaceErrorSuccess(t *testing.T) {
//Here 100000 errors are executed and but the limit is 100001
//Trying to find races with `go test -race`
tasks := make(chan Task)
times := 100000
gs := GreatestSearcher(times+1, tasks)
go func() {
tasks <- lazyAdder{adder{10}, 1}
for i := 0; i < times; i++ {
tasks <- lazyAdder{adder{127}, 1}
}
close(tasks)
}()
expResult := 20
result, err := gs.Execute(10)
if err != nil {
t.Errorf("Received an unexpected error %s", err)
} else if result != expResult {
t.Errorf("Received result %d when expecting %d", result, expResult)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment