Skip to content

Commit

Permalink
Use strategy pattern example
Browse files Browse the repository at this point in the history
  • Loading branch information
magaupp committed Nov 23, 2024
1 parent 24bbe01 commit 1f91174
Show file tree
Hide file tree
Showing 19 changed files with 465 additions and 42 deletions.
2 changes: 1 addition & 1 deletion src/main/resources/templates/aeolus/go/default.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set -e
test () {
echo '⚙️ executing test'
cd "${testWorkingDirectory}"
go test -json 2>&1 | go-junit-report -parser gojson -out test-results.xml
go test ./... -json 2>&1 | go-junit-report -parser gojson -out test-results.xml

}

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/aeolus/go/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ actions:
- name: test
script: |
cd "${testWorkingDirectory}"
go test -json 2>&1 | go-junit-report -parser gojson -out test-results.xml
go test ./... -json 2>&1 | go-junit-report -parser gojson -out test-results.xml
results:
- name: Go Test Results
path: "${testWorkingDirectory}/test-results.xml"
Expand Down
5 changes: 0 additions & 5 deletions src/main/resources/templates/go/exercise/assignment.go

This file was deleted.

7 changes: 7 additions & 0 deletions src/main/resources/templates/go/exercise/bubblesort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package assignment

type BubbleSort struct{}

func NewBubbleSort() *BubbleSort {
panic("not implemented")
}
59 changes: 51 additions & 8 deletions src/main/resources/templates/go/exercise/client/client.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,63 @@
package main

import (
"artemis/assignment"
"fmt"
"math/rand"
"time"
)

// Constants define iteration and random date generation bounds.
const (
Iterations = 10
RandomFloor = 5
RandomCeiling = 15
)

// main demonstrates the sorting process.
func main() {
var a, b int
// TODO: Init Context and Policy

// Run multiple times to simulate different sorting strategies
for i := 0; i < Iterations; i++ {
dates := createRandomDates()

fmt.Print("a: ")
fmt.Scanln(&a)
// TODO: Configure context

fmt.Print("b: ")
fmt.Scanln(&b)
fmt.Println("Unsorted Array of course dates:")
printDates(dates)
fmt.Println()

// TODO: Sort dates

fmt.Println("Sorted Array of course dates:")
printDates(dates)
fmt.Println()
}
}

sum := assignment.Add(a, b)
// createRandomDates generates a slice of random dates with size between RandomFloor and RandomCeiling.
func createRandomDates() []time.Time {
listLength := rand.Intn(RandomCeiling-RandomFloor) + RandomFloor
list := make([]time.Time, listLength)

lowestDate := time.Date(2024, 10, 15, 0, 0, 0, 0, time.UTC)
highestDate := time.Date(2025, 1, 15, 0, 0, 0, 0, time.UTC)

for i := 0; i < listLength; i++ {
list[i] = randomDateWithin(lowestDate, highestDate)
}
return list
}

// randomDateWithin creates a random time.Time value within the given range.
func randomDateWithin(low, high time.Time) time.Time {
randomTime := rand.Int63n(high.Unix()-low.Unix()) + low.Unix()
return time.Unix(randomTime, 0)
}

fmt.Printf("a + b = %d\n", sum)
// printDates prints out the given slice of time.Time values as dates.
func printDates(dates []time.Time) {
for _, date := range dates {
fmt.Println(date.Format(time.DateOnly))
}
}
7 changes: 7 additions & 0 deletions src/main/resources/templates/go/exercise/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package assignment

type Context struct{}

func NewContext() *Context {
panic("not implemented")
}
7 changes: 7 additions & 0 deletions src/main/resources/templates/go/exercise/mergesort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package assignment

type MergeSort struct{}

func NewMergeSort() *MergeSort {
panic("not implemented")
}
7 changes: 7 additions & 0 deletions src/main/resources/templates/go/exercise/policy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package assignment

type Policy struct{}

func NewPolicy(context *Context) *Policy {
panic("not implemented")
}
3 changes: 3 additions & 0 deletions src/main/resources/templates/go/exercise/sortstrategy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package assignment

type SortStrategy interface{}
5 changes: 0 additions & 5 deletions src/main/resources/templates/go/solution/assignment.go

This file was deleted.

20 changes: 20 additions & 0 deletions src/main/resources/templates/go/solution/bubblesort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package assignment

import "time"

type BubbleSort struct{}

func NewBubbleSort() *BubbleSort {
return new(BubbleSort)
}

// PerformSort implements bubble sort for BubbleSort
func (b BubbleSort) PerformSort(input []time.Time) {
for i := len(input) - 1; i >= 0; i-- {
for j := 0; j < i; j++ {
if input[j].After(input[j+1]) {
input[j], input[j+1] = input[j+1], input[j]
}
}
}
}
61 changes: 54 additions & 7 deletions src/main/resources/templates/go/solution/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,65 @@ package main
import (
"artemis/assignment"
"fmt"
"math/rand"
"time"
)

// Constants define iteration and random date generation bounds.
const (
Iterations = 10
RandomFloor = 5
RandomCeiling = 15
)

// main demonstrates the sorting process.
func main() {
var a, b int
// Init Context and Policy
context := assignment.NewContext()
policy := assignment.NewPolicy(context)

// Run multiple times to simulate different sorting strategies
for i := 0; i < Iterations; i++ {
dates := createRandomDates()

fmt.Print("a: ")
fmt.Scanln(&a)
context.SetDates(dates)
policy.Configure()

fmt.Print("b: ")
fmt.Scanln(&b)
fmt.Println("Unsorted Array of course dates:")
printDates(dates)
fmt.Println()

context.Sort()

fmt.Println("Sorted Array of course dates:")
printDates(dates)
fmt.Println()
}
}

sum := assignment.Add(a, b)
// createRandomDates generates a slice of random dates with size between RandomFloor and RandomCeiling.
func createRandomDates() []time.Time {
listLength := rand.Intn(RandomCeiling-RandomFloor) + RandomFloor
list := make([]time.Time, listLength)

lowestDate := time.Date(2024, 10, 15, 0, 0, 0, 0, time.UTC)
highestDate := time.Date(2025, 1, 15, 0, 0, 0, 0, time.UTC)

for i := 0; i < listLength; i++ {
list[i] = randomDateWithin(lowestDate, highestDate)
}
return list
}

// randomDateWithin creates a random time.Time value within the given range.
func randomDateWithin(low, high time.Time) time.Time {
randomTime := rand.Int63n(high.Unix()-low.Unix()) + low.Unix()
return time.Unix(randomTime, 0)
}

fmt.Printf("a + b = %d\n", sum)
// printDates prints out the given slice of time.Time values as dates.
func printDates(dates []time.Time) {
for _, date := range dates {
fmt.Println(date.Format(time.DateOnly))
}
}
43 changes: 43 additions & 0 deletions src/main/resources/templates/go/solution/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package assignment

import "time"

type Context struct {
dates []time.Time
sortAlgorithm SortStrategy
}

func NewContext() *Context {
return new(Context)
}

// GetDates returns the slice of dates in the context
func (c *Context) GetDates() []time.Time {
return c.dates
}

func (c *Context) SetDates(dates []time.Time) {
c.dates = dates
}

func (c *Context) GetSortAlgorithm() SortStrategy {
return c.sortAlgorithm
}

// SetSortAlgorithm sets the sorting strategy.
func (c *Context) SetSortAlgorithm(strategy SortStrategy) {
c.sortAlgorithm = strategy
}

// Sort runs the configured sort algorithm.
func (c *Context) Sort() {
if c.sortAlgorithm == nil {
panic("sortAlgorithm has not been set")
}

if c.dates == nil {
panic("dates has not been set")
}

c.sortAlgorithm.PerformSort(c.dates)
}
57 changes: 57 additions & 0 deletions src/main/resources/templates/go/solution/mergesort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package assignment

import "time"

type MergeSort struct{}

func NewMergeSort() *MergeSort {
return new(MergeSort)
}

// PerformSort is a wrapper method for the real MergeSort algorithm.
func (m MergeSort) PerformSort(input []time.Time) {
mergeSort(input, 0, len(input)-1)
}

// mergeSort recursively applies the MergeSort algorithm.
func mergeSort(input []time.Time, low, high int) {
if high-low < 1 {
return
}
mid := (low + high) / 2
mergeSort(input, low, mid)
mergeSort(input, mid+1, high)
merge(input, low, mid, high)
}

// merge merges two ranges within input defined from low to mid and from mid+1 to high.
func merge(input []time.Time, low, mid, high int) {
temp := make([]time.Time, high-low+1)
left, right, k := low, mid+1, 0

for left <= mid && right <= high {
if input[left].Before(input[right]) || input[left].Equal(input[right]) {
temp[k] = input[left]
left++
} else {
temp[k] = input[right]
right++
}
k++
}

for left <= mid {
temp[k] = input[left]
left++
k++
}
for right <= high {
temp[k] = input[right]
right++
k++
}

for i, val := range temp {
input[low+i] = val
}
}
20 changes: 20 additions & 0 deletions src/main/resources/templates/go/solution/policy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package assignment

type Policy struct {
context *Context
}

const datesSizeThreshold = 10

func NewPolicy(context *Context) *Policy {
return &Policy{context}
}

// Configure chooses a strategy depending on the number of date objects.
func (p *Policy) Configure() {
if len(p.context.GetDates()) > datesSizeThreshold {
p.context.SetSortAlgorithm(NewMergeSort())
} else {
p.context.SetSortAlgorithm(NewBubbleSort())
}
}
8 changes: 8 additions & 0 deletions src/main/resources/templates/go/solution/sortstrategy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package assignment

import "time"

type SortStrategy interface {
// PerformSort sorts a slice of dates.
PerformSort(input []time.Time)
}
15 changes: 0 additions & 15 deletions src/main/resources/templates/go/test/assignment_test.go

This file was deleted.

Loading

0 comments on commit 1f91174

Please sign in to comment.