mirror of https://github.com/01-edu/public.git
Xavier Petit
5 years ago
committed by
xpetit
653 changed files with 36888 additions and 0 deletions
@ -0,0 +1,23 @@
|
||||
# tests |
||||
|
||||
## Docker image |
||||
|
||||
The tests must be in a Docker image that will be executed with (among others) the following options : |
||||
|
||||
- `--read-only` Mount the container's root filesystem as read only |
||||
- `--user 1000:1000` Avoid to give root rights |
||||
- `--tmpfs /jail:size=200M,noatime,exec,nodev,nosuid,uid=1000,gid=1000,nr_inodes=5k,mode=1700` Mount a 200MB tmpfs directory to create files and run tests |
||||
- `--memory 500M` Memory limit of 500 MB |
||||
- `--cpus 2.0` Number of CPUs (2 threads) |
||||
- `--mount readonly,type=volume,source=student/username,destination=/app/student` The student's code is available in /app/student |
||||
- `--env HOME=/jail` Set the only writable folder as home directory |
||||
- `--env TMPDIR=/jail` Set the only writable folder as temporary directory |
||||
- `--env USERNAME=` Username |
||||
- `--env EXERCISE=` Exercise name |
||||
- `--env DOMAIN=` Domain name (e.g. gp.ynov-bordeaux.com) |
||||
- `--env EXPECTED_FILES=` A space-separated list of required files |
||||
|
||||
The username is the Gitea login. |
||||
No command or arguments are used, the entrypoint has to run the tests. |
||||
The exit status of the container will determine whether or not the test has passed. |
||||
Any output will be printed in the platform but not interpreted. |
@ -0,0 +1,7 @@
|
||||
FROM golang:1 |
||||
|
||||
RUN go get golang.org/x/tools/cmd/goimports |
||||
RUN go get github.com/01-edu/z01 |
||||
RUN go get github.com/01-edu/public/rc |
||||
COPY . /app |
||||
ENTRYPOINT ["/bin/bash", "/app/entrypoint.sh"] |
@ -0,0 +1,19 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
"github.com/01-edu/z01" |
||||
) |
||||
|
||||
func TestAbort(t *testing.T) { |
||||
arg := z01.MultRandInt() |
||||
arg = append(arg, z01.RandInt()) |
||||
for i := 0; i < 15; i++ { |
||||
z01.Challenge(t, student.Abort, solutions.Abort, arg[0], arg[1], arg[2], arg[3], arg[4]) |
||||
arg = z01.MultRandInt() |
||||
arg = append(arg, z01.RandInt()) |
||||
} |
||||
} |
@ -0,0 +1,21 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
solutions "./solutions" |
||||
student "./student" |
||||
"github.com/01-edu/z01" |
||||
"testing" |
||||
) |
||||
|
||||
func TestActiveBits(t *testing.T) { |
||||
args := []int{z01.RandIntBetween(2, 20)} |
||||
|
||||
for i := 0; i < 20; i++ { |
||||
args = append(args, z01.RandIntBetween(2, 20)) |
||||
} |
||||
|
||||
for _, v := range args { |
||||
z01.Challenge(t, student.ActiveBits, solutions.ActiveBits, v) |
||||
} |
||||
|
||||
} |
@ -0,0 +1,44 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"reflect" |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
func TestAdvancedSortWordArr(t *testing.T) { |
||||
var table [][]string |
||||
|
||||
for i := 0; i < 10; i++ { |
||||
table = append(table, z01.MultRandWords()) |
||||
} |
||||
|
||||
table = append(table, []string{"a", "A", "1", "b", "B", "2", "c", "C", "3"}) |
||||
|
||||
for _, org := range table { |
||||
//copy for using the solution function
|
||||
cp_sol := make([]string, len(org)) |
||||
//copy for using the student function
|
||||
cp_stu := make([]string, len(org)) |
||||
|
||||
copy(cp_sol, org) |
||||
copy(cp_stu, org) |
||||
|
||||
solutions.AdvancedSortWordArr(cp_sol, solutions.CompArray) |
||||
student.AdvancedSortWordArr(cp_stu, solutions.CompArray) |
||||
|
||||
if !reflect.DeepEqual(cp_stu, cp_sol) { |
||||
t.Errorf("%s(%v) == %v instead of %v\n", |
||||
"AdvancedSortWordArr", |
||||
org, |
||||
cp_stu, |
||||
cp_sol, |
||||
) |
||||
} |
||||
} |
||||
|
||||
} |
@ -0,0 +1,30 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
solutions "./solutions" |
||||
|
||||
student "./student" |
||||
"github.com/01-edu/z01" |
||||
) |
||||
|
||||
func TestAlphaCount(t *testing.T) { |
||||
var arr []string |
||||
for l := 0; l < 7; l++ { |
||||
a := z01.RandIntBetween(5, 20) |
||||
b := z01.RandASCII() |
||||
str := z01.RandStr(a, b) |
||||
arr = append(arr, str) |
||||
} |
||||
|
||||
arr = append(arr, " ") |
||||
|
||||
// example from the subject
|
||||
arr = append(arr, "Hello 78 World! 4455 /") |
||||
|
||||
for i := 0; i < len(arr); i++ { |
||||
z01.Challenge(t, student.AlphaCount, solutions.AlphaCount, arr[i]) |
||||
|
||||
} |
||||
} |
@ -0,0 +1,73 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
solutions "./solutions" |
||||
student "./student" |
||||
"github.com/01-edu/z01" |
||||
"testing" |
||||
) |
||||
|
||||
func TestAny(t *testing.T) { |
||||
functionsArray := []func(string) bool{solutions.IsNumeric, solutions.IsLower, solutions.IsUpper} |
||||
|
||||
type node struct { |
||||
f func(string) bool |
||||
arr []string |
||||
} |
||||
|
||||
table := []node{} |
||||
|
||||
for i := 0; i < 5; i++ { |
||||
|
||||
functionSelected := functionsArray[z01.RandIntBetween(0, len(functionsArray)-1)] |
||||
val := node{ |
||||
f: functionSelected, |
||||
arr: z01.MultRandWords(), |
||||
} |
||||
table = append(table, val) |
||||
|
||||
} |
||||
for i := 0; i < 5; i++ { |
||||
|
||||
val := node{ |
||||
f: solutions.IsNumeric, |
||||
arr: z01.MultRandDigit(), |
||||
} |
||||
table = append(table, val) |
||||
|
||||
} |
||||
|
||||
for i := 0; i < 5; i++ { |
||||
|
||||
val := node{ |
||||
f: solutions.IsLower, |
||||
arr: z01.MultRandLower(), |
||||
} |
||||
table = append(table, val) |
||||
|
||||
} |
||||
for i := 0; i < 5; i++ { |
||||
|
||||
val := node{ |
||||
f: solutions.IsUpper, |
||||
arr: z01.MultRandUpper(), |
||||
} |
||||
table = append(table, val) |
||||
|
||||
} |
||||
|
||||
table = append(table, |
||||
node{ |
||||
f: solutions.IsNumeric, |
||||
arr: []string{"Hello", "how", "are", "you"}, |
||||
}, |
||||
node{ |
||||
f: solutions.IsNumeric, |
||||
arr: []string{"This", "is", "4", "you"}, |
||||
}, |
||||
) |
||||
|
||||
for _, arg := range table { |
||||
z01.Challenge(t, student.Any, solutions.Any, arg.f, arg.arr) |
||||
} |
||||
} |
@ -0,0 +1,53 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"github.com/01-edu/z01" |
||||
"testing" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
func TestAppendRange(t *testing.T) { |
||||
|
||||
type node struct { |
||||
min int |
||||
max int |
||||
} |
||||
|
||||
table := []node{} |
||||
//15 random pairs of ints for a Valid Range
|
||||
|
||||
for i := 0; i < 15; i++ { |
||||
|
||||
minVal := z01.RandIntBetween(-10000000, 1000000) |
||||
gap := z01.RandIntBetween(1, 20) |
||||
val := node{ |
||||
min: minVal, |
||||
max: minVal + gap, |
||||
} |
||||
table = append(table, val) |
||||
} |
||||
//15 random pairs of ints with ||invalid range||
|
||||
for i := 0; i < 15; i++ { |
||||
|
||||
minVal := z01.RandIntBetween(-10000000, 1000000) |
||||
gap := z01.RandIntBetween(1, 20) |
||||
val := node{ |
||||
min: minVal, |
||||
max: minVal - gap, |
||||
} |
||||
table = append(table, val) |
||||
} |
||||
|
||||
table = append(table, |
||||
node{min: 0, max: 1}, |
||||
node{min: 0, max: 0}, |
||||
node{min: 5, max: 10}, |
||||
node{min: 10, max: 5}, |
||||
) |
||||
|
||||
for _, arg := range table { |
||||
z01.Challenge(t, student.AppendRange, solutions.AppendRange, arg.min, arg.max) |
||||
} |
||||
} |
@ -0,0 +1,39 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"strconv" |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
func TestAtoi(t *testing.T) { |
||||
table := make([]string, 30) |
||||
for i := range table { |
||||
table[i] = strconv.Itoa(z01.RandInt()) |
||||
} |
||||
table = append(table, |
||||
strconv.Itoa(z01.MinInt), |
||||
strconv.Itoa(z01.MaxInt), |
||||
"", |
||||
"-", |
||||
"+", |
||||
"0", |
||||
"+0", |
||||
"-Invalid123", |
||||
"--123", |
||||
"-+123", |
||||
"++123", |
||||
"123-", |
||||
"123+", |
||||
"123.", |
||||
"123.0", |
||||
"123a45", |
||||
) |
||||
for _, arg := range table { |
||||
z01.Challenge(t, student.Atoi, solutions.Atoi, arg) |
||||
} |
||||
} |
@ -0,0 +1,49 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
// this is the function that creates the TESTS
|
||||
func TestAtoiBase(t *testing.T) { |
||||
type node struct { |
||||
s string |
||||
base string |
||||
} |
||||
|
||||
table := []node{} |
||||
|
||||
// 15 random pairs of string numbers with valid bases
|
||||
for i := 0; i < 15; i++ { |
||||
validBaseToInput := solutions.RandomValidBase() |
||||
val := node{ |
||||
s: solutions.RandomStringFromBase(validBaseToInput), |
||||
base: validBaseToInput, |
||||
} |
||||
table = append(table, val) |
||||
} |
||||
// 15 random pairs of string numbers with invalid bases
|
||||
for i := 0; i < 15; i++ { |
||||
invalidBaseToInput := solutions.RandomInvalidBase() |
||||
val := node{ |
||||
s: "thisinputshouldnotmatter", |
||||
base: invalidBaseToInput, |
||||
} |
||||
table = append(table, val) |
||||
} |
||||
table = append(table, |
||||
node{s: "125", base: "0123456789"}, |
||||
node{s: "1111101", base: "01"}, |
||||
node{s: "7D", base: "0123456789ABCDEF"}, |
||||
node{s: "uoi", base: "choumi"}, |
||||
node{s: "bbbbbab", base: "-ab"}, |
||||
) |
||||
for _, arg := range table { |
||||
z01.Challenge(t, student.AtoiBase, solutions.AtoiBase, arg.s, arg.base) |
||||
} |
||||
} |
@ -0,0 +1,92 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"math/rand" |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
) |
||||
|
||||
//randomValidBase function is used to create the tests (input VALID bases here)
|
||||
func randomValidBase() string { |
||||
validBases := []string{ |
||||
"01", |
||||
"CHOUMIisDAcat!", |
||||
"choumi", |
||||
"0123456789", |
||||
"abc", "Zone01", |
||||
"0123456789ABCDEF", |
||||
"WhoAmI?", |
||||
} |
||||
index := rand.Intn(len(validBases)) |
||||
return validBases[index] |
||||
} |
||||
|
||||
//randomInvalidBase function is used to create the tests (input INVALID bases here)
|
||||
func randomInvalidBase() string { |
||||
invalidBases := []string{ |
||||
"0", |
||||
"1", |
||||
"CHOUMIisdacat!", |
||||
"choumiChoumi", |
||||
"01234567890", |
||||
"abca", |
||||
"Zone01Zone01", |
||||
"0123456789ABCDEF0", |
||||
"WhoAmI?IamWhoIam", |
||||
} |
||||
index := z01.RandIntBetween(0, len(invalidBases)-1) |
||||
return invalidBases[index] |
||||
} |
||||
|
||||
//randomStringFromBase function is used to create the random STRING number from VALID BASES
|
||||
func randomStringFromBase(base string) string { |
||||
letters := []rune(base) |
||||
size := z01.RandIntBetween(1, 10) |
||||
r := make([]rune, size) |
||||
for i := range r { |
||||
r[i] = letters[rand.Intn(len(letters))] |
||||
} |
||||
return string(r) |
||||
} |
||||
|
||||
// this is the function that creates the TESTS
|
||||
func TestAtoiBaseProg(t *testing.T) { |
||||
type node struct { |
||||
s string |
||||
base string |
||||
} |
||||
|
||||
table := []node{} |
||||
|
||||
// 5 random pairs of string numbers with valid bases
|
||||
for i := 0; i < 5; i++ { |
||||
validBaseToInput := randomValidBase() |
||||
val := node{ |
||||
s: randomStringFromBase(validBaseToInput), |
||||
base: validBaseToInput, |
||||
} |
||||
table = append(table, val) |
||||
} |
||||
// 5 random pairs of string numbers with invalid bases
|
||||
for i := 0; i < 5; i++ { |
||||
invalidBaseToInput := randomInvalidBase() |
||||
val := node{ |
||||
s: "thisinputshouldnotmatter", |
||||
base: invalidBaseToInput, |
||||
} |
||||
table = append(table, val) |
||||
} |
||||
table = append(table, |
||||
node{s: "125", base: "0123456789"}, |
||||
node{s: "1111101", base: "01"}, |
||||
node{s: "7D", base: "0123456789ABCDEF"}, |
||||
node{s: "uoi", base: "choumi"}, |
||||
node{s: "bbbbbab", base: "-ab"}, |
||||
) |
||||
for _, arg := range table { |
||||
z01.ChallengeMain(t, arg.s, arg.base) |
||||
} |
||||
z01.ChallengeMain(t) |
||||
z01.ChallengeMain(t, "125", "0123456789", "something") |
||||
} |
@ -0,0 +1,33 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"strconv" |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
func TestBasicAtoi2(t *testing.T) { |
||||
table := make([]string, 30) |
||||
for i := range table { |
||||
table[i] = strconv.Itoa(z01.RandPosZ()) |
||||
} |
||||
table = append(table, |
||||
strconv.Itoa(z01.MaxInt), |
||||
"", |
||||
"0", |
||||
"Invalid123", |
||||
"123Invalid", |
||||
"Invalid", |
||||
"1Invalid23", |
||||
"123", |
||||
"123.", |
||||
"123.0", |
||||
) |
||||
for _, arg := range table { |
||||
z01.Challenge(t, student.BasicAtoi2, solutions.BasicAtoi2, arg) |
||||
} |
||||
} |
@ -0,0 +1,29 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"strconv" |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
func TestBasicAtoi(t *testing.T) { |
||||
table := make([]string, 30) |
||||
for i := range table { |
||||
table[i] = strconv.Itoa(z01.RandPosZ()) |
||||
} |
||||
table = append(table, |
||||
strconv.Itoa(z01.MaxInt), |
||||
"", |
||||
"0", |
||||
"12345", |
||||
"0000012345", |
||||
"000000", |
||||
) |
||||
for _, arg := range table { |
||||
z01.Challenge(t, student.BasicAtoi, solutions.BasicAtoi, arg) |
||||
} |
||||
} |
@ -0,0 +1,25 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
func TestBasicJoin(t *testing.T) { |
||||
table := [][]string{} |
||||
|
||||
// 30 valid pair of ramdom slice of strings to concatenate
|
||||
for i := 0; i < 30; i++ { |
||||
table = append(table, z01.MultRandASCII()) |
||||
} |
||||
table = append(table, |
||||
[]string{"Hello!", " How are you?", "well and yourself?"}, |
||||
) |
||||
for _, arg := range table { |
||||
z01.Challenge(t, student.BasicJoin, solutions.BasicJoin, arg) |
||||
} |
||||
} |
@ -0,0 +1,11 @@
|
||||
#!/usr/bin/env bash |
||||
|
||||
set -euo pipefail |
||||
IFS=' |
||||
' |
||||
|
||||
cd -P "$(dirname "$BASH_SOURCE")" |
||||
|
||||
rm -rf student |
||||
cp -a solutions student |
||||
go test -v -json|jq -c 'select(.Action == "pass") | {Test, Elapsed}' | jq -sr 'sort_by(.Elapsed) | .[-30:] | .[] | [.Elapsed, .Test] | @tsv' |
@ -0,0 +1,17 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"strings" |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
) |
||||
|
||||
func TestBoolean(t *testing.T) { |
||||
|
||||
table := append(z01.MultRandWords(), "1 2 3 4 5") |
||||
|
||||
for _, s := range table { |
||||
z01.ChallengeMain(t, strings.Fields(s)...) |
||||
} |
||||
} |
@ -0,0 +1,23 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"fmt" |
||||
"testing" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
func TestBTreeApplyByLevel(t *testing.T) { |
||||
root := &solutions.TreeNode{Data: "04"} |
||||
rootS := &student.TreeNode{Data: "04"} |
||||
|
||||
ins := []string{"01", "07", "05", "12", "02", "03", "10"} |
||||
|
||||
for _, v := range ins { |
||||
root = solutions.BTreeInsertData(root, v) |
||||
rootS = student.BTreeInsertData(rootS, v) |
||||
} |
||||
|
||||
solutions.ChallengeTree(t, solutions.BTreeApplyByLevel, student.BTreeApplyByLevel, root, rootS, fmt.Print) |
||||
} |
@ -0,0 +1,36 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"fmt" |
||||
"testing" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
func TestBTreeApplyInorder(t *testing.T) { |
||||
root := &solutions.TreeNode{Data: "08"} |
||||
rootS := &student.TreeNode{Data: "08"} |
||||
var pos []string |
||||
|
||||
pos = append(pos, |
||||
"x", |
||||
"z", |
||||
"y", |
||||
"t", |
||||
"r", |
||||
"q", |
||||
"01", |
||||
"b", |
||||
"c", |
||||
"a", |
||||
"d", |
||||
) |
||||
|
||||
for _, arg := range pos { |
||||
root = solutions.BTreeInsertData(root, arg) |
||||
rootS = student.BTreeInsertData(rootS, arg) |
||||
} |
||||
|
||||
solutions.ChallengeTree(t, solutions.BTreeApplyInorder, student.BTreeApplyInorder, root, rootS, fmt.Println) |
||||
} |
@ -0,0 +1,36 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"fmt" |
||||
"testing" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
func TestBTreeApplyPostorder(t *testing.T) { |
||||
root := &solutions.TreeNode{Data: "08"} |
||||
rootS := &student.TreeNode{Data: "08"} |
||||
var pos []string |
||||
|
||||
pos = append(pos, |
||||
"x", |
||||
"z", |
||||
"y", |
||||
"t", |
||||
"r", |
||||
"q", |
||||
"01", |
||||
"b", |
||||
"c", |
||||
"a", |
||||
"d", |
||||
) |
||||
|
||||
for _, arg := range pos { |
||||
root = solutions.BTreeInsertData(root, arg) |
||||
rootS = student.BTreeInsertData(rootS, arg) |
||||
} |
||||
|
||||
solutions.ChallengeTree(t, solutions.BTreeApplyPostorder, student.BTreeApplyPostorder, root, rootS, fmt.Println) |
||||
} |
@ -0,0 +1,36 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"fmt" |
||||
"testing" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
func TestBTreeApplyPreorder(t *testing.T) { |
||||
root := &solutions.TreeNode{Data: "08"} |
||||
rootS := &student.TreeNode{Data: "08"} |
||||
var pos []string |
||||
|
||||
pos = append(pos, |
||||
"x", |
||||
"z", |
||||
"y", |
||||
"t", |
||||
"r", |
||||
"q", |
||||
"01", |
||||
"b", |
||||
"c", |
||||
"a", |
||||
"d", |
||||
) |
||||
|
||||
for _, arg := range pos { |
||||
root = solutions.BTreeInsertData(root, arg) |
||||
rootS = student.BTreeInsertData(rootS, arg) |
||||
} |
||||
|
||||
solutions.ChallengeTree(t, solutions.BTreeApplyPreorder, student.BTreeApplyPreorder, root, rootS, fmt.Println) |
||||
} |
@ -0,0 +1,123 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
func parentListDelete(root *student.TreeNode) string { |
||||
if root == nil { |
||||
return "" |
||||
} |
||||
|
||||
var parent string |
||||
|
||||
if root.Parent == nil { |
||||
parent = "nil" |
||||
} else { |
||||
parent = root.Parent.Data |
||||
} |
||||
|
||||
r := "Node: " + root.Data + " Parent: " + parent + "\n" |
||||
r += parentListDelete(root.Left) + parentListDelete(root.Right) |
||||
return r |
||||
} |
||||
|
||||
func FormatTree_delete(root *student.TreeNode) string { |
||||
if root == nil { |
||||
return "" |
||||
} |
||||
res := root.Data + "\n" |
||||
res += formatSubTree_delete(root, "") |
||||
return res |
||||
} |
||||
|
||||
func formatSubTree_delete(root *student.TreeNode, prefix string) string { |
||||
if root == nil { |
||||
return "" |
||||
} |
||||
|
||||
var res string |
||||
|
||||
hasLeft := root.Left != nil |
||||
hasRight := root.Right != nil |
||||
|
||||
if !hasLeft && !hasRight { |
||||
return res |
||||
} |
||||
|
||||
res += prefix |
||||
if hasLeft && hasRight { |
||||
res += "├── " |
||||
} |
||||
|
||||
if !hasLeft && hasRight { |
||||
res += "└── " |
||||
} |
||||
|
||||
if hasRight { |
||||
printStrand := (hasLeft && hasRight && (root.Right.Right != nil || root.Right.Left != nil)) |
||||
newPrefix := prefix |
||||
if printStrand { |
||||
newPrefix += "│ " |
||||
} else { |
||||
newPrefix += " " |
||||
} |
||||
res += root.Right.Data + "\n" |
||||
res += formatSubTree_delete(root.Right, newPrefix) |
||||
} |
||||
|
||||
if hasLeft { |
||||
if hasRight { |
||||
res += prefix |
||||
} |
||||
res += "└── " + root.Left.Data + "\n" |
||||
res += formatSubTree_delete(root.Left, prefix+" ") |
||||
} |
||||
return res |
||||
} |
||||
|
||||
func errorMessage_delete(t *testing.T, fn interface{}, deleted string, rootOr, root *solutions.TreeNode, rootS *student.TreeNode) { |
||||
t.Errorf("%s(\n%s, %s\n) ==\n%s instead of\n%s\n", |
||||
z01.NameOfFunc(fn), |
||||
solutions.FormatTree(rootOr), |
||||
deleted, |
||||
FormatTree_delete(rootS), |
||||
solutions.FormatTree(root), |
||||
) |
||||
} |
||||
|
||||
func CompareTrees_delete(t *testing.T, fn interface{}, deleted string, rootOr, root *solutions.TreeNode, rootS *student.TreeNode) { |
||||
sel := student.BTreeSearchItem(rootS, deleted) |
||||
|
||||
if !student.BTreeIsBinary(rootS) || sel != nil { |
||||
errorMessage_delete(t, fn, deleted, rootOr, root, rootS) |
||||
} |
||||
|
||||
} |
||||
|
||||
func TestBTreeDeleteNode(t *testing.T) { |
||||
root := &solutions.TreeNode{Data: "04"} |
||||
rootS := &student.TreeNode{Data: "04"} |
||||
rootOr := &solutions.TreeNode{Data: "04"} |
||||
|
||||
ins := []string{"01", "07", "05", "12", "02", "03", "10"} |
||||
|
||||
for _, v := range ins { |
||||
root = solutions.BTreeInsertData(root, v) |
||||
rootS = student.BTreeInsertData(rootS, v) |
||||
rootOr = solutions.BTreeInsertData(rootOr, v) |
||||
} |
||||
|
||||
selected := solutions.BTreeSearchItem(root, "04") |
||||
selectedS := student.BTreeSearchItem(rootS, "04") |
||||
|
||||
root = solutions.BTreeDeleteNode(root, selected) |
||||
rootS = student.BTreeDeleteNode(rootS, selectedS) |
||||
fn := interface{}(solutions.BTreeDeleteNode) |
||||
CompareTrees_delete(t, fn, selected.Data, rootOr, root, rootS) |
||||
} |
@ -0,0 +1,129 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
func parentListInsert(root *student.TreeNode) string { |
||||
if root == nil { |
||||
return "" |
||||
} |
||||
|
||||
var parent string |
||||
|
||||
if root.Parent == nil { |
||||
parent = "nil" |
||||
} else { |
||||
parent = root.Parent.Data |
||||
} |
||||
|
||||
r := "Node: " + root.Data + " Parent: " + parent + "\n" |
||||
r += parentListInsert(root.Left) + parentListInsert(root.Right) |
||||
return r |
||||
} |
||||
|
||||
func FormatTree_insert(root *student.TreeNode) string { |
||||
if root == nil { |
||||
return "" |
||||
} |
||||
res := root.Data + "\n" |
||||
res += formatSubTree_insert(root, "") |
||||
return res |
||||
} |
||||
|
||||
func formatSubTree_insert(root *student.TreeNode, prefix string) string { |
||||
if root == nil { |
||||
return "" |
||||
} |
||||
|
||||
var res string |
||||
|
||||
hasLeft := root.Left != nil |
||||
hasRight := root.Right != nil |
||||
|
||||
if !hasLeft && !hasRight { |
||||
return res |
||||
} |
||||
|
||||
res += prefix |
||||
if hasLeft && hasRight { |
||||
res += "├── " |
||||
} |
||||
|
||||
if !hasLeft && hasRight { |
||||
res += "└── " |
||||
} |
||||
|
||||
if hasRight { |
||||
printStrand := (hasLeft && hasRight && (root.Right.Right != nil || root.Right.Left != nil)) |
||||
newPrefix := prefix |
||||
if printStrand { |
||||
newPrefix += "│ " |
||||
} else { |
||||
newPrefix += " " |
||||
} |
||||
res += root.Right.Data + "\n" |
||||
res += formatSubTree_insert(root.Right, newPrefix) |
||||
} |
||||
|
||||
if hasLeft { |
||||
if hasRight { |
||||
res += prefix |
||||
} |
||||
res += "└── " + root.Left.Data + "\n" |
||||
res += formatSubTree_insert(root.Left, prefix+" ") |
||||
} |
||||
return res |
||||
} |
||||
|
||||
func errorMessage_insert(t *testing.T, fn interface{}, inserted string, root *solutions.TreeNode, rootS *student.TreeNode) { |
||||
t.Errorf("%s(\n%s, %s\n) ==\n%s instead of\n%s\n", |
||||
z01.NameOfFunc(fn), |
||||
solutions.FormatTree(root), |
||||
inserted, |
||||
FormatTree_insert(rootS), |
||||
solutions.FormatTree(root), |
||||
) |
||||
} |
||||
|
||||
func CompareTrees_insert(t *testing.T, fn interface{}, inserted string, root *solutions.TreeNode, rootS *student.TreeNode) { |
||||
solTree := solutions.FormatTree(root) |
||||
stuTree := FormatTree_insert(rootS) |
||||
|
||||
if solTree != stuTree { |
||||
errorMessage_insert(t, fn, inserted, root, rootS) |
||||
} |
||||
} |
||||
|
||||
func TestBTreeInsertData(t *testing.T) { |
||||
root := &solutions.TreeNode{Data: "08"} |
||||
rootS := &student.TreeNode{Data: "08"} |
||||
|
||||
var pos []string |
||||
|
||||
pos = append(pos, |
||||
"x", |
||||
"z", |
||||
"y", |
||||
"t", |
||||
"r", |
||||
"q", |
||||
"01", |
||||
"b", |
||||
"c", |
||||
"a", |
||||
"d", |
||||
) |
||||
fn := interface{}(solutions.BTreeInsertData) |
||||
for _, arg := range pos { |
||||
root = solutions.BTreeInsertData(root, arg) |
||||
rootS = student.BTreeInsertData(rootS, arg) |
||||
CompareTrees_insert(t, fn, arg, root, rootS) |
||||
} |
||||
|
||||
} |
@ -0,0 +1,120 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"reflect" |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
func BTreeMinStu(root *student.TreeNode) *student.TreeNode { |
||||
if root == nil || root.Left == nil { |
||||
return root |
||||
} |
||||
|
||||
return BTreeMinStu(root.Left) |
||||
} |
||||
|
||||
func errorMessage_isbin(t *testing.T, fn interface{}, root, a *solutions.TreeNode, b *student.TreeNode) { |
||||
t.Errorf("%s(\n%s\n) == %s instead of %s\n", |
||||
z01.NameOfFunc(fn), |
||||
solutions.FormatTree(root), |
||||
b.Data, |
||||
a.Data, |
||||
) |
||||
} |
||||
|
||||
func CompareNode_isbin(t *testing.T, fn interface{}, arg1, a *solutions.TreeNode, b *student.TreeNode) { |
||||
if a == nil || b == nil { |
||||
t.Errorf("Expected %v instead of %v\n", a, b) |
||||
return |
||||
} |
||||
|
||||
if a.Data != b.Data { |
||||
errorMessage_isbin(t, fn, arg1, a, b) |
||||
} |
||||
|
||||
if a.Parent != nil && b.Parent != nil { |
||||
if a.Parent.Data != b.Parent.Data { |
||||
errorMessage_isbin(t, fn, arg1, a, b) |
||||
t.Errorf("Expected parent value %v instead of %v\n", a.Parent.Data, b.Parent.Data) |
||||
} |
||||
} else if (a.Parent == nil && b.Parent != nil) || (a.Parent != nil && b.Parent == nil) { |
||||
t.Errorf("Expected parent value %v instead of %v\n", a.Parent, b.Parent) |
||||
} |
||||
|
||||
if a.Right != nil && b.Right != nil { |
||||
if a.Right.Data != b.Right.Data { |
||||
errorMessage_isbin(t, fn, arg1, a, b) |
||||
t.Errorf("Expected right child value %v instead of %v\n", a.Right.Data, b.Right.Data) |
||||
} |
||||
} else if (a.Right == nil && b.Right != nil) || (a.Right != nil && b.Right == nil) { |
||||
t.Errorf("Expected right child value %v instead of %v\n", a.Right, b.Right) |
||||
} |
||||
|
||||
if a.Left != nil && b.Left != nil { |
||||
if a.Left.Data != b.Left.Data { |
||||
errorMessage_isbin(t, fn, arg1, a, b) |
||||
t.Errorf("Expected left child value %v instead of %v\n", a.Left, b.Left) |
||||
} |
||||
} else if (a.Left == nil && b.Left != nil) || (a.Left != nil && b.Left == nil) { |
||||
t.Errorf("Expected left child value %v instead of %v\n", a, b) |
||||
} |
||||
} |
||||
|
||||
func CompareReturn_isbin(t *testing.T, fn1, fn2 interface{}, arg1 *solutions.TreeNode, arg2 interface{}) { |
||||
arar1 := []interface{}{arg1} |
||||
arar2 := []interface{}{arg2} |
||||
|
||||
out1 := z01.Monitor(fn1, arar1) |
||||
out2 := z01.Monitor(fn2, arar2) |
||||
|
||||
for i, v := range out1.Results { |
||||
switch str := v.(type) { |
||||
case *solutions.TreeNode: |
||||
CompareNode_isbin(t, fn1, arg1, str, out2.Results[i].(*student.TreeNode)) |
||||
default: |
||||
if !reflect.DeepEqual(str, out2.Results[i]) { |
||||
t.Errorf("%s(\n%s) == %s instead of %s\n", |
||||
z01.NameOfFunc(fn1), |
||||
solutions.FormatTree(arg1), |
||||
z01.Format(out2.Results...), |
||||
z01.Format(out1.Results...), |
||||
) |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
func TestBTreeIsBinary(t *testing.T) { |
||||
root := &solutions.TreeNode{Data: "04"} |
||||
rootS := &student.TreeNode{Data: "04"} |
||||
|
||||
ins := []string{"01", "07", "05", "12", "02", "03", "10"} |
||||
|
||||
for _, v := range ins { |
||||
root = solutions.BTreeInsertData(root, v) |
||||
rootS = student.BTreeInsertData(rootS, v) |
||||
} |
||||
|
||||
CompareReturn_isbin(t, solutions.BTreeIsBinary, student.BTreeIsBinary, root, rootS) |
||||
|
||||
rootNB := &solutions.TreeNode{Data: "04"} |
||||
rootNB_stu := &student.TreeNode{Data: "04"} |
||||
//Test a non-binarysearch tree
|
||||
for _, v := range ins { |
||||
rootNB = solutions.BTreeInsertData(rootNB, v) |
||||
rootNB_stu = student.BTreeInsertData(rootNB_stu, v) |
||||
} |
||||
|
||||
min := solutions.BTreeMin(rootNB) |
||||
minStu := BTreeMinStu(rootNB_stu) |
||||
|
||||
min.Left = &solutions.TreeNode{Data: "123"} |
||||
minStu.Left = &student.TreeNode{Data: "123"} |
||||
|
||||
CompareReturn_isbin(t, solutions.BTreeIsBinary, student.BTreeIsBinary, rootNB, rootNB_stu) |
||||
} |
@ -0,0 +1,95 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"reflect" |
||||
"testing" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
|
||||
"github.com/01-edu/z01" |
||||
) |
||||
|
||||
func errorMessage_level(t *testing.T, fn interface{}, root, a *solutions.TreeNode, b *student.TreeNode) { |
||||
t.Errorf("%s(\n%s\n) == %s instead of %s\n", |
||||
z01.NameOfFunc(fn), |
||||
solutions.FormatTree(root), |
||||
b.Data, |
||||
a.Data, |
||||
) |
||||
} |
||||
|
||||
func CompareNode_level(t *testing.T, fn interface{}, arg1, a *solutions.TreeNode, b *student.TreeNode) { |
||||
if a == nil || b == nil { |
||||
t.Errorf("Expected %v instead of %v\n", a, b) |
||||
return |
||||
} |
||||
|
||||
if a.Data != b.Data { |
||||
errorMessage_level(t, fn, arg1, a, b) |
||||
} |
||||
|
||||
if a.Parent != nil && b.Parent != nil { |
||||
if a.Parent.Data != b.Parent.Data { |
||||
errorMessage_level(t, fn, arg1, a, b) |
||||
t.Errorf("Expected parent value %v instead of %v\n", a.Parent.Data, b.Parent.Data) |
||||
} |
||||
} else if (a.Parent == nil && b.Parent != nil) || (a.Parent != nil && b.Parent == nil) { |
||||
t.Errorf("Expected parent value %v instead of %v\n", a.Parent, b.Parent) |
||||
} |
||||
|
||||
if a.Right != nil && b.Right != nil { |
||||
if a.Right.Data != b.Right.Data { |
||||
errorMessage_level(t, fn, arg1, a, b) |
||||
t.Errorf("Expected right child value %v instead of %v\n", a.Right.Data, b.Right.Data) |
||||
} |
||||
} else if (a.Right == nil && b.Right != nil) || (a.Right != nil && b.Right == nil) { |
||||
t.Errorf("Expected right child value %v instead of %v\n", a.Right, b.Right) |
||||
} |
||||
|
||||
if a.Left != nil && b.Left != nil { |
||||
if a.Left.Data != b.Left.Data { |
||||
errorMessage_level(t, fn, arg1, a, b) |
||||
t.Errorf("Expected left child value %v instead of %v\n", a.Left, b.Left) |
||||
} |
||||
} else if (a.Left == nil && b.Left != nil) || (a.Left != nil && b.Left == nil) { |
||||
t.Errorf("Expected left child value %v instead of %v\n", a, b) |
||||
} |
||||
} |
||||
|
||||
func CompareReturn_level(t *testing.T, fn1, fn2 interface{}, arg1 *solutions.TreeNode, arg2 interface{}) { |
||||
arar1 := []interface{}{arg1} |
||||
arar2 := []interface{}{arg2} |
||||
|
||||
out1 := z01.Monitor(fn1, arar1) |
||||
out2 := z01.Monitor(fn2, arar2) |
||||
|
||||
for i, v := range out1.Results { |
||||
switch str := v.(type) { |
||||
case *solutions.TreeNode: |
||||
CompareNode_level(t, fn1, arg1, str, out2.Results[i].(*student.TreeNode)) |
||||
default: |
||||
if !reflect.DeepEqual(str, out2.Results[i]) { |
||||
t.Errorf("%s(\n%s) == %s instead of %s\n", |
||||
z01.NameOfFunc(fn1), |
||||
solutions.FormatTree(arg1), |
||||
z01.Format(out2.Results...), |
||||
z01.Format(out1.Results...), |
||||
) |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
func TestBTreeLevelCount(t *testing.T) { |
||||
root := &solutions.TreeNode{Data: "04"} |
||||
rootS := &student.TreeNode{Data: "04"} |
||||
|
||||
ins := []string{"01", "07", "05", "12", "02", "03", "10"} |
||||
|
||||
for _, v := range ins { |
||||
root = solutions.BTreeInsertData(root, v) |
||||
rootS = student.BTreeInsertData(rootS, v) |
||||
CompareReturn_level(t, solutions.BTreeLevelCount, student.BTreeLevelCount, root, rootS) |
||||
} |
||||
} |
@ -0,0 +1,96 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"reflect" |
||||
"testing" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
|
||||
"github.com/01-edu/z01" |
||||
) |
||||
|
||||
func errorMessage_max(t *testing.T, fn interface{}, root, a *solutions.TreeNode, b *student.TreeNode) { |
||||
t.Errorf("%s(\n%s) == %s instead of %s\n", |
||||
z01.NameOfFunc(fn), |
||||
solutions.FormatTree(root), |
||||
b.Data, |
||||
a.Data, |
||||
) |
||||
} |
||||
|
||||
func CompareNode_max(t *testing.T, fn interface{}, arg1, a *solutions.TreeNode, b *student.TreeNode) { |
||||
if a == nil || b == nil { |
||||
t.Errorf("Expected %v instead of %v\n", a, b) |
||||
return |
||||
} |
||||
|
||||
if a.Data != b.Data { |
||||
errorMessage_max(t, fn, arg1, a, b) |
||||
} |
||||
|
||||
if a.Parent != nil && b.Parent != nil { |
||||
if a.Parent.Data != b.Parent.Data { |
||||
errorMessage_max(t, fn, arg1, a, b) |
||||
t.Errorf("Expected parent value %v instead of %v\n", a.Parent.Data, b.Parent.Data) |
||||
} |
||||
} else if (a.Parent == nil && b.Parent != nil) || (a.Parent != nil && b.Parent == nil) { |
||||
t.Errorf("Expected parent value %v instead of %v\n", a.Parent, b.Parent) |
||||
} |
||||
|
||||
if a.Right != nil && b.Right != nil { |
||||
if a.Right.Data != b.Right.Data { |
||||
errorMessage_max(t, fn, arg1, a, b) |
||||
t.Errorf("Expected right child value %v instead of %v\n", a.Right.Data, b.Right.Data) |
||||
} |
||||
} else if (a.Right == nil && b.Right != nil) || (a.Right != nil && b.Right == nil) { |
||||
t.Errorf("Expected right child value %v instead of %v\n", a.Right, b.Right) |
||||
} |
||||
|
||||
if a.Left != nil && b.Left != nil { |
||||
if a.Left.Data != b.Left.Data { |
||||
errorMessage_max(t, fn, arg1, a, b) |
||||
t.Errorf("Expected left child value %v instead of %v\n", a.Left, b.Left) |
||||
} |
||||
} else if (a.Left == nil && b.Left != nil) || (a.Left != nil && b.Left == nil) { |
||||
t.Errorf("Expected left child value %v instead of %v\n", a, b) |
||||
} |
||||
} |
||||
|
||||
func CompareReturn_max(t *testing.T, fn1, fn2 interface{}, arg1 *solutions.TreeNode, arg2 interface{}) { |
||||
arar1 := []interface{}{arg1} |
||||
arar2 := []interface{}{arg2} |
||||
|
||||
out1 := z01.Monitor(fn1, arar1) |
||||
out2 := z01.Monitor(fn2, arar2) |
||||
|
||||
for i, v := range out1.Results { |
||||
switch str := v.(type) { |
||||
case *solutions.TreeNode: |
||||
CompareNode_max(t, fn1, arg1, str, out2.Results[i].(*student.TreeNode)) |
||||
default: |
||||
if !reflect.DeepEqual(str, out2.Results[i]) { |
||||
t.Errorf("%s(\n%s) == %s instead of\n %s\n", |
||||
z01.NameOfFunc(fn1), |
||||
solutions.FormatTree(arg1), |
||||
z01.Format(out2.Results...), |
||||
z01.Format(out1.Results...), |
||||
) |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
func TestBTreeMax(t *testing.T) { |
||||
root := &solutions.TreeNode{Data: "04"} |
||||
rootS := &student.TreeNode{Data: "04"} |
||||
|
||||
ins := []string{"01", "07", "05", "12", "02", "03", "10"} |
||||
|
||||
for _, v := range ins { |
||||
root = solutions.BTreeInsertData(root, v) |
||||
rootS = student.BTreeInsertData(rootS, v) |
||||
} |
||||
|
||||
CompareReturn_max(t, solutions.BTreeMax, student.BTreeMax, root, rootS) |
||||
} |
@ -0,0 +1,96 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"reflect" |
||||
"testing" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
|
||||
"github.com/01-edu/z01" |
||||
) |
||||
|
||||
func errorMessage_min(t *testing.T, fn interface{}, root, a *solutions.TreeNode, b *student.TreeNode) { |
||||
t.Errorf("%s(\n%s) == %s instead of %s\n", |
||||
z01.NameOfFunc(fn), |
||||
solutions.FormatTree(root), |
||||
b.Data, |
||||
a.Data, |
||||
) |
||||
} |
||||
|
||||
func CompareNode_min(t *testing.T, fn interface{}, arg1, a *solutions.TreeNode, b *student.TreeNode) { |
||||
if a == nil || b == nil { |
||||
t.Errorf("Expected %v instead of %v\n", a, b) |
||||
return |
||||
} |
||||
|
||||
if a.Data != b.Data { |
||||
errorMessage_min(t, fn, arg1, a, b) |
||||
} |
||||
|
||||
if a.Parent != nil && b.Parent != nil { |
||||
if a.Parent.Data != b.Parent.Data { |
||||
errorMessage_min(t, fn, arg1, a, b) |
||||
t.Errorf("Expected parent value %v instead of %v\n", a.Parent.Data, b.Parent.Data) |
||||
} |
||||
} else if (a.Parent == nil && b.Parent != nil) || (a.Parent != nil && b.Parent == nil) { |
||||
t.Errorf("Expected parent value %v instead of %v\n", a.Parent, b.Parent) |
||||
} |
||||
|
||||
if a.Right != nil && b.Right != nil { |
||||
if a.Right.Data != b.Right.Data { |
||||
errorMessage_min(t, fn, arg1, a, b) |
||||
t.Errorf("Expected right child value %v instead of %v\n", a.Right.Data, b.Right.Data) |
||||
} |
||||
} else if (a.Right == nil && b.Right != nil) || (a.Right != nil && b.Right == nil) { |
||||
t.Errorf("Expected right child value %v instead of %v\n", a.Right, b.Right) |
||||
} |
||||
|
||||
if a.Left != nil && b.Left != nil { |
||||
if a.Left.Data != b.Left.Data { |
||||
errorMessage_min(t, fn, arg1, a, b) |
||||
t.Errorf("Expected left child value %v instead of %v\n", a.Left, b.Left) |
||||
} |
||||
} else if (a.Left == nil && b.Left != nil) || (a.Left != nil && b.Left == nil) { |
||||
t.Errorf("Expected left child value %v instead of %v\n", a, b) |
||||
} |
||||
} |
||||
|
||||
func CompareReturn_min(t *testing.T, fn1, fn2, arg1, arg2 interface{}) { |
||||
arar1 := []interface{}{arg1} |
||||
arar2 := []interface{}{arg2} |
||||
|
||||
out1 := z01.Monitor(fn1, arar1) |
||||
out2 := z01.Monitor(fn2, arar2) |
||||
|
||||
for i, v := range out1.Results { |
||||
switch str := v.(type) { |
||||
case *solutions.TreeNode: |
||||
CompareNode_min(t, fn1, arg1.(*solutions.TreeNode), str, out2.Results[i].(*student.TreeNode)) |
||||
default: |
||||
if !reflect.DeepEqual(str, out2.Results[i]) { |
||||
t.Errorf("%s(%s) == %s instead of %s\n", |
||||
z01.NameOfFunc(fn1), |
||||
z01.Format(arg1), |
||||
z01.Format(out2.Results...), |
||||
z01.Format(out1.Results...), |
||||
) |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
func TestBTreeMin(t *testing.T) { |
||||
root := &solutions.TreeNode{Data: "04"} |
||||
rootS := &student.TreeNode{Data: "04"} |
||||
|
||||
ins := []string{"03", "02", "01", "07", "05", "12", "10"} |
||||
|
||||
for _, v := range ins { |
||||
root = solutions.BTreeInsertData(root, v) |
||||
rootS = student.BTreeInsertData(rootS, v) |
||||
} |
||||
|
||||
CompareReturn_min(t, solutions.BTreeMin, student.BTreeMin, root, rootS) |
||||
} |
@ -0,0 +1,152 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"reflect" |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
func parentListRotLeft(root *student.TreeNode) string { |
||||
if root == nil { |
||||
return "" |
||||
} |
||||
|
||||
var parent string |
||||
|
||||
if root.Parent == nil { |
||||
parent = "nil" |
||||
} else { |
||||
parent = root.Parent.Data |
||||
} |
||||
|
||||
r := "Node: " + root.Data + " Parent: " + parent + "\n" |
||||
r += parentListRotLeft(root.Left) + parentListRotLeft(root.Right) |
||||
return r |
||||
} |
||||
|
||||
func FormatTree_rotleft(root *student.TreeNode) string { |
||||
if root == nil { |
||||
return "" |
||||
} |
||||
res := root.Data + "\n" |
||||
res += formatSubTree_rotleft(root, "") |
||||
return res |
||||
} |
||||
|
||||
func formatSubTree_rotleft(root *student.TreeNode, prefix string) string { |
||||
if root == nil { |
||||
return "" |
||||
} |
||||
|
||||
var res string |
||||
|
||||
hasLeft := root.Left != nil |
||||
hasRight := root.Right != nil |
||||
|
||||
if !hasLeft && !hasRight { |
||||
return res |
||||
} |
||||
|
||||
res += prefix |
||||
if hasLeft && hasRight { |
||||
res += "├── " |
||||
} |
||||
|
||||
if !hasLeft && hasRight { |
||||
res += "└── " |
||||
} |
||||
|
||||
if hasRight { |
||||
printStrand := (hasLeft && hasRight && (root.Right.Right != nil || root.Right.Left != nil)) |
||||
newPrefix := prefix |
||||
if printStrand { |
||||
newPrefix += "│ " |
||||
} else { |
||||
newPrefix += " " |
||||
} |
||||
res += root.Right.Data + "\n" |
||||
res += formatSubTree_rotleft(root.Right, newPrefix) |
||||
} |
||||
|
||||
if hasLeft { |
||||
if hasRight { |
||||
res += prefix |
||||
} |
||||
res += "└── " + root.Left.Data + "\n" |
||||
res += formatSubTree_rotleft(root.Left, prefix+" ") |
||||
} |
||||
return res |
||||
} |
||||
|
||||
func errorMessage_rotleft(t *testing.T, fn interface{}, rootOr, root *solutions.TreeNode, rootS *student.TreeNode) { |
||||
t.Errorf("%s(\n%s\n)\n == \n%s instead of \n%s\n", |
||||
z01.NameOfFunc(fn), |
||||
solutions.FormatTree(rootOr), |
||||
FormatTree_rotleft(rootS), |
||||
solutions.FormatTree(root), |
||||
) |
||||
} |
||||
|
||||
func CompareTreesRotLeft(t *testing.T, fn interface{}, root, rootA *solutions.TreeNode, rootAS *student.TreeNode) { |
||||
parentSol := solutions.ParentList(rootA) |
||||
parentStu := parentListRotLeft(rootAS) |
||||
solTree := solutions.FormatTree(root) |
||||
if parentSol != parentStu { |
||||
t.Errorf("Tree:\n%s\nExpected\n%s instead of\n%s\n", solTree, parentSol, parentStu) |
||||
} |
||||
} |
||||
|
||||
func CompareNode_rotleft(t *testing.T, fn interface{}, rootOr, root *solutions.TreeNode, rootS *student.TreeNode) { |
||||
solTree := solutions.FormatTree(root) |
||||
stuTree := FormatTree_rotleft(rootS) |
||||
|
||||
if solTree != stuTree { |
||||
errorMessage_rotleft(t, fn, rootOr, root, rootS) |
||||
} |
||||
} |
||||
|
||||
func CompareReturn_rotleft(t *testing.T, fn1, fn2, rootOr, arg1, arg2 interface{}) { |
||||
arar1 := []interface{}{arg1} |
||||
arar2 := []interface{}{arg2} |
||||
|
||||
out1 := z01.Monitor(fn1, arar1) |
||||
out2 := z01.Monitor(fn2, arar2) |
||||
|
||||
for i, v := range out1.Results { |
||||
switch str := v.(type) { |
||||
case *solutions.TreeNode: |
||||
CompareNode_rotleft(t, fn1, rootOr.(*solutions.TreeNode), str, out2.Results[i].(*student.TreeNode)) |
||||
default: |
||||
if !reflect.DeepEqual(str, out2.Results[i]) { |
||||
t.Errorf("%s(%s) == %s instead of %s\n", |
||||
z01.NameOfFunc(fn1), |
||||
z01.Format(arg1), |
||||
z01.Format(out2.Results...), |
||||
z01.Format(out1.Results...), |
||||
) |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
func TestBTreeRotateLeft(t *testing.T) { |
||||
root := &solutions.TreeNode{Data: "04"} |
||||
rootOr := &solutions.TreeNode{Data: "04"} |
||||
rootS := &student.TreeNode{Data: "04"} |
||||
|
||||
ins := []string{"01", "07", "05", "12", "02", "03", "10"} |
||||
|
||||
for _, v := range ins { |
||||
root = solutions.BTreeInsertData(root, v) |
||||
rootS = student.BTreeInsertData(rootS, v) |
||||
rootOr = solutions.BTreeInsertData(rootOr, v) |
||||
} |
||||
|
||||
fn := interface{}(solutions.BTreeRotateLeft) |
||||
CompareReturn_rotleft(t, fn, student.BTreeRotateLeft, rootOr, root, rootS) |
||||
CompareTreesRotLeft(t, fn, rootOr, root, rootS) |
||||
} |
@ -0,0 +1,151 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"reflect" |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
func parentListRotRight(root *student.TreeNode) string { |
||||
if root == nil { |
||||
return "" |
||||
} |
||||
|
||||
var parent string |
||||
|
||||
if root.Parent == nil { |
||||
parent = "nil" |
||||
} else { |
||||
parent = root.Parent.Data |
||||
} |
||||
|
||||
r := "Node: " + root.Data + " Parent: " + parent + "\n" |
||||
r += parentListRotRight(root.Left) + parentListRotRight(root.Right) |
||||
return r |
||||
} |
||||
|
||||
func CompareTreesRotRight(t *testing.T, fn interface{}, root, rootA *solutions.TreeNode, rootAS *student.TreeNode) { |
||||
parentSol := solutions.ParentList(rootA) |
||||
parentStu := parentListRotRight(rootAS) |
||||
solTree := solutions.FormatTree(root) |
||||
if parentSol != parentStu { |
||||
t.Errorf("Tree:\n%s\nExpected\n%s instead of\n%s\n", solTree, parentSol, parentStu) |
||||
} |
||||
} |
||||
|
||||
func FormatTree_rotright(root *student.TreeNode) string { |
||||
if root == nil { |
||||
return "" |
||||
} |
||||
res := root.Data + "\n" |
||||
res += formatSubTree_rotright(root, "") |
||||
return res |
||||
} |
||||
|
||||
func formatSubTree_rotright(root *student.TreeNode, prefix string) string { |
||||
if root == nil { |
||||
return "" |
||||
} |
||||
|
||||
var res string |
||||
|
||||
hasLeft := root.Left != nil |
||||
hasRight := root.Right != nil |
||||
|
||||
if !hasLeft && !hasRight { |
||||
return res |
||||
} |
||||
|
||||
res += prefix |
||||
if hasLeft && hasRight { |
||||
res += "├── " |
||||
} |
||||
|
||||
if !hasLeft && hasRight { |
||||
res += "└── " |
||||
} |
||||
|
||||
if hasRight { |
||||
printStrand := (hasLeft && hasRight && (root.Right.Right != nil || root.Right.Left != nil)) |
||||
newPrefix := prefix |
||||
if printStrand { |
||||
newPrefix += "│ " |
||||
} else { |
||||
newPrefix += " " |
||||
} |
||||
res += root.Right.Data + "\n" |
||||
res += formatSubTree_rotright(root.Right, newPrefix) |
||||
} |
||||
|
||||
if hasLeft { |
||||
if hasRight { |
||||
res += prefix |
||||
} |
||||
res += "└── " + root.Left.Data + "\n" |
||||
res += formatSubTree_rotright(root.Left, prefix+" ") |
||||
} |
||||
return res |
||||
} |
||||
|
||||
func errorMessage_rotright(t *testing.T, fn interface{}, rootOr, root *solutions.TreeNode, rootS *student.TreeNode) { |
||||
t.Errorf("%s(\n%s\n)\n == \n%s instead of \n%s\n", |
||||
z01.NameOfFunc(fn), |
||||
solutions.FormatTree(rootOr), |
||||
FormatTree_rotright(rootS), |
||||
solutions.FormatTree(root), |
||||
) |
||||
} |
||||
|
||||
func CompareNode_rotright(t *testing.T, fn interface{}, rootOr, root *solutions.TreeNode, rootS *student.TreeNode) { |
||||
solTree := solutions.FormatTree(root) |
||||
stuTree := FormatTree_rotright(rootS) |
||||
|
||||
if solTree != stuTree { |
||||
errorMessage_rotright(t, fn, rootOr, root, rootS) |
||||
} |
||||
} |
||||
|
||||
func CompareReturn_rotright(t *testing.T, fn1, fn2, rootOr, arg1, arg2 interface{}) { |
||||
arar1 := []interface{}{arg1} |
||||
arar2 := []interface{}{arg2} |
||||
|
||||
out1 := z01.Monitor(fn1, arar1) |
||||
out2 := z01.Monitor(fn2, arar2) |
||||
|
||||
for i, v := range out1.Results { |
||||
switch str := v.(type) { |
||||
case *solutions.TreeNode: |
||||
CompareNode_rotright(t, fn1, rootOr.(*solutions.TreeNode), str, out2.Results[i].(*student.TreeNode)) |
||||
default: |
||||
if !reflect.DeepEqual(str, out2.Results[i]) { |
||||
t.Errorf("%s(%s) == %s instead of %s\n", |
||||
z01.NameOfFunc(fn1), |
||||
z01.Format(arg1), |
||||
z01.Format(out2.Results...), |
||||
z01.Format(out1.Results...), |
||||
) |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
func TestBTreeRotateRight(t *testing.T) { |
||||
root := &solutions.TreeNode{Data: "04"} |
||||
rootOr := &solutions.TreeNode{Data: "04"} |
||||
rootS := &student.TreeNode{Data: "04"} |
||||
|
||||
ins := []string{"01", "07", "05", "12", "02", "03", "10"} |
||||
|
||||
for _, v := range ins { |
||||
root = solutions.BTreeInsertData(root, v) |
||||
rootS = student.BTreeInsertData(rootS, v) |
||||
rootOr = solutions.BTreeInsertData(rootOr, v) |
||||
} |
||||
fn := interface{}(solutions.BTreeRotateRight) |
||||
CompareReturn_rotright(t, fn, student.BTreeRotateRight, rootOr, root, rootS) |
||||
CompareTreesRotRight(t, fn, rootOr, root, rootS) |
||||
} |
@ -0,0 +1,90 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
"fmt" |
||||
) |
||||
|
||||
func errorMessage_search(t *testing.T, fn interface{}, root, a *solutions.TreeNode, b *student.TreeNode, |
||||
seaVal string) { |
||||
t.Errorf("%s(\n%s\n, %s) == %s instead of %s\n", |
||||
z01.NameOfFunc(fn), |
||||
solutions.FormatTree(root), |
||||
seaVal, |
||||
b.Data, |
||||
a.Data, |
||||
) |
||||
} |
||||
|
||||
func CompareNode_search(t *testing.T, fn interface{}, arg1, a *solutions.TreeNode, b *student.TreeNode, |
||||
seaVal string) { |
||||
|
||||
if a == nil && b == nil { |
||||
return |
||||
} |
||||
|
||||
if (a == nil && b != nil) || (b == nil && a != nil) { |
||||
t.Errorf("Expected %v instead of %v\n", a, b) |
||||
return |
||||
} |
||||
|
||||
if a.Data != b.Data { |
||||
errorMessage_search(t, fn, arg1, a, b, seaVal) |
||||
} |
||||
|
||||
if a.Parent != nil && b.Parent != nil { |
||||
if a.Parent.Data != b.Parent.Data { |
||||
errorMessage_search(t, fn, arg1, a, b, seaVal) |
||||
t.Errorf("Expected parent value %v instead of %v\n", a.Parent.Data, b.Parent.Data) |
||||
fmt.Println("Parent.Data", a.Parent.Data, b.Parent.Data) |
||||
} |
||||
} else if (a.Parent == nil && b.Parent != nil) || (a.Parent != nil && b.Parent == nil) { |
||||
t.Errorf("Expected parent value %v instead of %v\n", a.Parent, b.Parent) |
||||
} |
||||
|
||||
if a.Right != nil && b.Right != nil { |
||||
if a.Right.Data != b.Right.Data { |
||||
errorMessage_search(t, fn, arg1, a, b, seaVal) |
||||
t.Errorf("Expected right child value %v instead of %v\n", a.Right.Data, b.Right.Data) |
||||
fmt.Println("Right.Data", a.Right.Data, b.Right.Data) |
||||
} |
||||
} else if (a.Right == nil && b.Right != nil) || (a.Right != nil && b.Right == nil) { |
||||
t.Errorf("Expected right child value %v instead of %v\n", a.Right, b.Right) |
||||
} |
||||
|
||||
if a.Left != nil && b.Left != nil { |
||||
if a.Left.Data != b.Left.Data { |
||||
errorMessage_search(t, fn, arg1, a, b, seaVal) |
||||
t.Errorf("Expected left child value %v instead of %v\n", a.Left, b.Left) |
||||
fmt.Println("Left.Data", a.Left.Data, b.Left.Data) |
||||
} |
||||
} else if (a.Left == nil && b.Left != nil) || (a.Left != nil && b.Left == nil) { |
||||
t.Errorf("Expected left child value %v instead of %v\n", a, b) |
||||
} |
||||
} |
||||
|
||||
func TestBTreeSearchItem(t *testing.T) { |
||||
root := &solutions.TreeNode{Data: "04"} |
||||
rootS := &student.TreeNode{Data: "04"} |
||||
|
||||
ins := []string{"01", "07", "05", "12", "02", "03", "10"} |
||||
|
||||
for _, v := range ins { |
||||
root = solutions.BTreeInsertData(root, v) |
||||
rootS = student.BTreeInsertData(rootS, v) |
||||
} |
||||
fn := interface{}(solutions.BTreeSearchItem) |
||||
|
||||
ins = append(ins, "322") |
||||
for _, v := range ins { |
||||
selectedSol := solutions.BTreeSearchItem(root, v) |
||||
selectedStu := student.BTreeSearchItem(rootS, v) |
||||
CompareNode_search(t, fn, root, selectedSol, selectedStu, v) |
||||
} |
||||
|
||||
} |
@ -0,0 +1,142 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
"fmt" |
||||
) |
||||
|
||||
func parentListTransp(root *student.TreeNode) string { |
||||
if root == nil { |
||||
return "" |
||||
} |
||||
|
||||
var parent string |
||||
|
||||
if root.Parent == nil { |
||||
parent = "nil" |
||||
} else { |
||||
parent = root.Parent.Data |
||||
} |
||||
|
||||
r := "Node: " + root.Data + " Parent: " + parent + "\n" |
||||
r += parentListTransp(root.Left) + parentListTransp(root.Right) |
||||
return r |
||||
} |
||||
|
||||
func FormatTree_transp(root *student.TreeNode) string { |
||||
if root == nil { |
||||
return "" |
||||
} |
||||
res := root.Data + "\n" |
||||
res += formatSubTree_transp(root, "") |
||||
return res |
||||
} |
||||
|
||||
func formatSubTree_transp(root *student.TreeNode, prefix string) string { |
||||
if root == nil { |
||||
return "" |
||||
} |
||||
|
||||
var res string |
||||
|
||||
hasLeft := root.Left != nil |
||||
hasRight := root.Right != nil |
||||
|
||||
if !hasLeft && !hasRight { |
||||
return res |
||||
} |
||||
|
||||
res += prefix |
||||
if hasLeft && hasRight { |
||||
res += "├── " |
||||
} |
||||
|
||||
if !hasLeft && hasRight { |
||||
res += "└── " |
||||
} |
||||
|
||||
if hasRight { |
||||
printStrand := (hasLeft && hasRight && (root.Right.Right != nil || root.Right.Left != nil)) |
||||
newPrefix := prefix |
||||
if printStrand { |
||||
newPrefix += "│ " |
||||
} else { |
||||
newPrefix += " " |
||||
} |
||||
res += root.Right.Data + "\n" |
||||
res += formatSubTree_transp(root.Right, newPrefix) |
||||
} |
||||
|
||||
if hasLeft { |
||||
if hasRight { |
||||
res += prefix |
||||
} |
||||
res += "└── " + root.Left.Data + "\n" |
||||
res += formatSubTree_transp(root.Left, prefix+" ") |
||||
} |
||||
return res |
||||
} |
||||
|
||||
func errorMessage_transp(t *testing.T, fn interface{}, root, sel, repl *solutions.TreeNode, rootA *solutions.TreeNode, rootAS *student.TreeNode) { |
||||
t.Errorf("%s(\nRoot:\n %s, Selected:\n%s, Replacement:\n%s\n) ==\n%s instead of\n%s\n", |
||||
z01.NameOfFunc(fn), |
||||
solutions.FormatTree(root), |
||||
solutions.FormatTree(sel), |
||||
solutions.FormatTree(repl), |
||||
FormatTree_transp(rootAS), |
||||
solutions.FormatTree(rootA), |
||||
) |
||||
} |
||||
|
||||
func CompareTrees_transp(t *testing.T, fn interface{}, root, sel, repl *solutions.TreeNode, rootA *solutions.TreeNode, rootAS *student.TreeNode) { |
||||
solTree := solutions.FormatTree(rootA) |
||||
stuTree := FormatTree_transp(rootAS) |
||||
|
||||
if solTree != stuTree { |
||||
errorMessage_transp(t, fn, root, sel, repl, rootA, rootAS) |
||||
} |
||||
parentSol := solutions.ParentList(rootA) |
||||
parentStu := parentListTransp(rootAS) |
||||
|
||||
if parentSol != parentStu { |
||||
fmt.Println("Tree:\n", solTree) |
||||
t.Errorf("Expected\n%s instead of\n%s\n", parentSol, parentStu) |
||||
} |
||||
} |
||||
|
||||
func TestBTreeTransplant(t *testing.T) { |
||||
root := &solutions.TreeNode{Data: "04"} |
||||
rootS := &student.TreeNode{Data: "04"} |
||||
rootOr := &solutions.TreeNode{Data: "04"} |
||||
|
||||
replacement := &solutions.TreeNode{Data: "55"} |
||||
replacementS := &student.TreeNode{Data: "55"} |
||||
|
||||
ins := []string{"01", "07", "05", "12", "02", "03", "10"} |
||||
|
||||
rep := []string{"33", "12", "15", "60"} |
||||
|
||||
for _, v := range ins { |
||||
root = solutions.BTreeInsertData(root, v) |
||||
rootS = student.BTreeInsertData(rootS, v) |
||||
rootOr = solutions.BTreeInsertData(rootOr, v) |
||||
} |
||||
|
||||
for _, v := range rep { |
||||
replacement = solutions.BTreeInsertData(replacement, v) |
||||
replacementS = student.BTreeInsertData(replacementS, v) |
||||
} |
||||
|
||||
selected := solutions.BTreeSearchItem(root, "07") |
||||
selectedS := student.BTreeSearchItem(rootS, "07") |
||||
root = solutions.BTreeTransplant(root, selected, replacement) |
||||
rootS = student.BTreeTransplant(rootS, selectedS, replacementS) |
||||
fn := interface{}(solutions.BTreeTransplant) |
||||
|
||||
CompareTrees_transp(t, fn, rootOr, selected, replacement, root, rootS) |
||||
} |
@ -0,0 +1,26 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
func TestCapitalize(t *testing.T) { |
||||
table := append( |
||||
z01.MultRandASCII(), |
||||
"Hello! How are you? How+are+things+4you?", |
||||
"Hello! How are you?", |
||||
"a", |
||||
"z", |
||||
"!", |
||||
"9a", |
||||
"9a LALALA!", |
||||
) |
||||
for _, arg := range table { |
||||
z01.Challenge(t, student.Capitalize, solutions.Capitalize, arg) |
||||
} |
||||
} |
@ -0,0 +1,93 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"errors" |
||||
"io/ioutil" |
||||
"log" |
||||
"os" |
||||
"os/exec" |
||||
"strings" |
||||
"testing" |
||||
|
||||
solutions "./solutions" |
||||
|
||||
"github.com/01-edu/z01" |
||||
) |
||||
|
||||
//executes commands
|
||||
func execC(name string, args ...string) (string, error) { |
||||
out, err := exec.Command(name, args...).Output() |
||||
|
||||
output := string(out) |
||||
if err == nil { |
||||
return output, nil |
||||
} |
||||
if output == "" { |
||||
return "", z01.Wrap(err, "Command failed") |
||||
} |
||||
return "", errors.New(output) |
||||
} |
||||
|
||||
func TestCat(t *testing.T) { |
||||
|
||||
var table []string |
||||
pathFileName := "./student/cat/quest8.txt" |
||||
pathFileName2 := "./student/cat/quest8T.txt" |
||||
|
||||
solutions.CheckFile(t, pathFileName) |
||||
solutions.CheckFile(t, pathFileName2) |
||||
|
||||
table = append(table, pathFileName, pathFileName+" "+pathFileName2, "asd") |
||||
|
||||
for _, s := range table { |
||||
z01.ChallengeMain(t, strings.Fields(s)...) |
||||
} |
||||
_, err := execC("go", "build", "-o", "cat_student", "./student/cat/main.go") |
||||
_, err = execC("go", "build", "-o", "cat_solution", "./solutions/cat/main.go") |
||||
if err != nil { |
||||
log.Fatal(err.Error()) |
||||
} |
||||
pwd, err := os.Getwd() |
||||
|
||||
if err != nil { |
||||
t.Errorf(err.Error()) |
||||
} |
||||
|
||||
for i := 0; i < 2; i++ { |
||||
randStdin := z01.RandAlnum() |
||||
cmd := exec.Command("sh", "-c", pwd+"/cat_solution") |
||||
solutionResult := execStdin(cmd, randStdin) |
||||
cmdS := exec.Command(pwd + "/cat_student") |
||||
studentResult := execStdin(cmdS, randStdin) |
||||
|
||||
if solutionResult != studentResult { |
||||
t.Errorf("./cat prints %s instead of %s\n", studentResult, solutionResult) |
||||
} |
||||
} |
||||
execC("rm", "cat_student", "cat_solution") |
||||
} |
||||
|
||||
func execStdin(cmd *exec.Cmd, randomStdin string) string { |
||||
stdin, err := cmd.StdinPipe() |
||||
if err != nil { |
||||
log.Fatal(err) |
||||
} |
||||
stdout, err := cmd.StdoutPipe() |
||||
if err != nil { |
||||
log.Fatal(err) |
||||
} |
||||
if err := cmd.Start(); err != nil { |
||||
log.Fatal(err) |
||||
} |
||||
_, err = stdin.Write([]byte(randomStdin)) |
||||
if err != nil { |
||||
log.Fatal(err) |
||||
} |
||||
stdin.Close() |
||||
|
||||
out, _ := ioutil.ReadAll(stdout) |
||||
if err := cmd.Wait(); err != nil { |
||||
log.Fatal(err) |
||||
} |
||||
return string(out) |
||||
} |
@ -0,0 +1,21 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
"github.com/01-edu/z01" |
||||
) |
||||
|
||||
func TestCollatzCountdown(t *testing.T) { |
||||
args := []int{z01.RandIntBetween(-6, 20)} |
||||
args = append(args, -5, 0) |
||||
for i := 0; i < 20; i++ { |
||||
args = append(args, z01.RandIntBetween(2, 20)) |
||||
} |
||||
|
||||
for _, v := range args { |
||||
z01.Challenge(t, student.CollatzCountdown, solutions.CollatzCountdown, v) |
||||
} |
||||
} |
@ -0,0 +1,23 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"strings" |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
) |
||||
|
||||
func TestComCheck(t *testing.T) { |
||||
table := append(z01.MultRandWords(), |
||||
"01", |
||||
"galaxy", |
||||
"galaxy01", |
||||
" 01", |
||||
"as das d 01 asd", |
||||
"as galaxy d 12", |
||||
"as ds galaxy 01 asd") |
||||
|
||||
for _, s := range table { |
||||
z01.ChallengeMain(t, strings.Fields(s)...) |
||||
} |
||||
} |
@ -0,0 +1,59 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"reflect" |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
func TestCompact(t *testing.T) { |
||||
var arg [][]string |
||||
|
||||
for i := 0; i < 20; i++ { |
||||
n := z01.RandIntBetween(5, 20) // random size of the slice
|
||||
|
||||
orig := make([]string, n) //slice with the original value
|
||||
|
||||
num_pos := z01.RandIntBetween(1, n-1) // random number of positions to be written
|
||||
|
||||
for i := 0; i < num_pos; i++ { |
||||
word := z01.RandWords() // random string value
|
||||
rand_pos := z01.RandIntBetween(0, n-1) // random position in the slice
|
||||
orig[rand_pos] = word |
||||
} |
||||
arg = append(arg, orig) |
||||
} |
||||
|
||||
arg = append(arg, []string{"hello", "", "hi", "", "salut", "", ""}) |
||||
|
||||
for _, v := range arg { |
||||
sli_sol := make([]string, len(arg)) // slice to apply the solution function
|
||||
sli_stu := make([]string, len(arg)) // slice to apply the student function
|
||||
|
||||
copy(sli_sol, v) |
||||
copy(sli_stu, v) |
||||
|
||||
sol_size := solutions.Compact(&sli_sol) |
||||
stu_size := student.Compact(&sli_stu) |
||||
|
||||
if !reflect.DeepEqual(sli_stu, sli_sol) { |
||||
t.Errorf("Produced slice: %v, instead of %v\n", |
||||
sli_stu, |
||||
sli_sol, |
||||
) |
||||
} |
||||
|
||||
if sol_size != stu_size { |
||||
t.Errorf("%s(%v) == %v instead of %v\n", |
||||
"Compact", |
||||
v, |
||||
sli_stu, |
||||
sli_sol, |
||||
) |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,53 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
func TestCompare(t *testing.T) { |
||||
type node struct { |
||||
s string |
||||
toCompare string |
||||
} |
||||
|
||||
table := []node{} |
||||
|
||||
// the first 15 values are returning 0 for this test
|
||||
for i := 0; i < 15; i++ { |
||||
wordToTest := z01.RandASCII() |
||||
|
||||
val := node{ |
||||
s: wordToTest, |
||||
toCompare: wordToTest, |
||||
} |
||||
table = append(table, val) |
||||
} |
||||
|
||||
// the next 15 values are supposed to return 1 or -1 for this test
|
||||
for i := 0; i < 15; i++ { |
||||
wordToTest := z01.RandASCII() |
||||
wrongMatch := z01.RandASCII() |
||||
|
||||
val := node{ |
||||
s: wordToTest, |
||||
toCompare: wrongMatch, |
||||
} |
||||
table = append(table, val) |
||||
|
||||
} |
||||
// those are the test values from the README examples
|
||||
table = append(table, |
||||
node{s: "Hello!", toCompare: "Hello!"}, |
||||
node{s: "Salut!", toCompare: "lut!"}, |
||||
node{s: "Ola!", toCompare: "Ol"}, |
||||
) |
||||
|
||||
for _, arg := range table { |
||||
z01.Challenge(t, student.Compare, solutions.Compare, arg.s, arg.toCompare) |
||||
} |
||||
} |
@ -0,0 +1,26 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
func TestConcat(t *testing.T) { |
||||
table := [][]string{} |
||||
|
||||
// 30 valid pair of ramdom strings to concatenate
|
||||
for i := 0; i < 30; i++ { |
||||
value := []string{z01.RandASCII(), z01.RandASCII()} |
||||
table = append(table, value) |
||||
} |
||||
table = append(table, |
||||
[]string{"Hello!", " How are you?"}, |
||||
) |
||||
for _, arg := range table { |
||||
z01.Challenge(t, student.Concat, solutions.Concat, arg[0], arg[1]) |
||||
} |
||||
} |
@ -0,0 +1,29 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
func TestConcatParams(t *testing.T) { |
||||
|
||||
table := [][]string{} |
||||
//30 random slice of strings
|
||||
|
||||
for i := 0; i < 30; i++ { |
||||
val := z01.MultRandASCII() |
||||
table = append(table, val) |
||||
} |
||||
|
||||
table = append(table, |
||||
[]string{"Hello", "how", "are", "you?"}, |
||||
) |
||||
|
||||
for _, arg := range table { |
||||
z01.Challenge(t, student.ConcatParams, solutions.ConcatParams, arg) |
||||
} |
||||
} |
@ -0,0 +1,42 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
func TestConvertBase(t *testing.T) { |
||||
|
||||
type node struct { |
||||
nbr string |
||||
baseFrom string |
||||
baseTo string |
||||
} |
||||
table := []node{} |
||||
|
||||
for i := 0; i < 30; i++ { |
||||
validBaseToInput1 := solutions.RandomValidBase() |
||||
validBaseToInput2 := solutions.RandomValidBase() |
||||
str := solutions.ConvertNbrBase(z01.RandIntBetween(0, 1000000), validBaseToInput1) |
||||
val := node{ |
||||
nbr: str, |
||||
baseFrom: validBaseToInput1, |
||||
baseTo: validBaseToInput2, |
||||
} |
||||
table = append(table, val) |
||||
} |
||||
|
||||
table = append(table, node{ |
||||
nbr: "101011", |
||||
baseFrom: "01", |
||||
baseTo: "0123456789"}, |
||||
) |
||||
|
||||
for _, arg := range table { |
||||
z01.Challenge(t, student.ConvertBase, solutions.ConvertBase, arg.nbr, arg.baseFrom, arg.baseTo) |
||||
} |
||||
} |
@ -0,0 +1,75 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
func TestCountIf(t *testing.T) { |
||||
functionsArray := []func(string) bool{solutions.IsNumeric, solutions.IsLower, solutions.IsUpper} |
||||
|
||||
type node struct { |
||||
f func(string) bool |
||||
arr []string |
||||
} |
||||
|
||||
table := []node{} |
||||
|
||||
for i := 0; i < 5; i++ { |
||||
|
||||
functionSelected := functionsArray[z01.RandIntBetween(0, len(functionsArray)-1)] |
||||
val := node{ |
||||
f: functionSelected, |
||||
arr: z01.MultRandWords(), |
||||
} |
||||
table = append(table, val) |
||||
|
||||
} |
||||
for i := 0; i < 5; i++ { |
||||
|
||||
val := node{ |
||||
f: solutions.IsNumeric, |
||||
arr: z01.MultRandDigit(), |
||||
} |
||||
table = append(table, val) |
||||
|
||||
} |
||||
|
||||
for i := 0; i < 5; i++ { |
||||
|
||||
val := node{ |
||||
f: solutions.IsLower, |
||||
arr: z01.MultRandLower(), |
||||
} |
||||
table = append(table, val) |
||||
|
||||
} |
||||
for i := 0; i < 5; i++ { |
||||
|
||||
val := node{ |
||||
f: solutions.IsUpper, |
||||
arr: z01.MultRandUpper(), |
||||
} |
||||
table = append(table, val) |
||||
|
||||
} |
||||
|
||||
table = append(table, |
||||
node{ |
||||
f: solutions.IsNumeric, |
||||
arr: []string{"Hello", "how", "are", "you"}, |
||||
}, |
||||
node{ |
||||
f: solutions.IsNumeric, |
||||
arr: []string{"This", "is", "4", "you"}, |
||||
}, |
||||
) |
||||
|
||||
for _, arg := range table { |
||||
z01.Challenge(t, student.CountIf, solutions.CountIf, arg.f, arg.arr) |
||||
} |
||||
} |
@ -0,0 +1,50 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
solution "./solutions" |
||||
student "./student" |
||||
"github.com/01-edu/z01" |
||||
) |
||||
|
||||
//the structs from the other packages
|
||||
//struct just for the first exercise
|
||||
type NodeF = student.Node |
||||
type NodeFS = solution.Node |
||||
|
||||
//exercise 1
|
||||
//simple struct, just insert a element in the struct
|
||||
func TestCreateElem(t *testing.T) { |
||||
n := &NodeFS{} |
||||
n1 := &NodeF{} |
||||
|
||||
type nodeTest struct { |
||||
data []int |
||||
} |
||||
|
||||
table := []nodeTest{} |
||||
|
||||
for i := 0; i < 5; i++ { |
||||
val := nodeTest{ |
||||
data: z01.MultRandIntBetween(-1000000, 10000000), |
||||
} |
||||
table = append(table, val) |
||||
} |
||||
|
||||
table = append(table, |
||||
nodeTest{ |
||||
data: []int{132423}, |
||||
}, |
||||
) |
||||
for _, arg := range table { |
||||
for i := 0; i < len(arg.data); i++ { |
||||
solution.CreateElem(n, arg.data[i]) |
||||
student.CreateElem(n1, arg.data[i]) |
||||
} |
||||
|
||||
if n.Data != n1.Data { |
||||
t.Errorf("CreateElem == %d instead of %d\n", n, n1) |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,23 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"strings" |
||||
"testing" |
||||
|
||||
solutions "./solutions" |
||||
"github.com/01-edu/z01" |
||||
) |
||||
|
||||
func TestDisplayfile(t *testing.T) { |
||||
|
||||
var table []string |
||||
pathFileName := "./student/displayfile/quest8.txt" |
||||
|
||||
solutions.CheckFile(t, pathFileName) |
||||
|
||||
table = append(table, "", pathFileName, "quest8.txt asdsada") |
||||
|
||||
for _, s := range table { |
||||
z01.ChallengeMain(t, strings.Fields(s)...) |
||||
} |
||||
} |
@ -0,0 +1,27 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
|
||||
student "./student" |
||||
) |
||||
|
||||
func TestDivMod(t *testing.T) { |
||||
i := 0 |
||||
for i < z01.SliceLen { |
||||
a := z01.RandInt() |
||||
b := z01.RandInt() |
||||
var div int |
||||
var mod int |
||||
student.DivMod(a, b, &div, &mod) |
||||
if div != a/b { |
||||
t.Errorf("DivMod(%d, %d, &div, &mod), div == %d instead of %d", a, b, div, a/b) |
||||
} |
||||
if mod != a%b { |
||||
t.Errorf("DivMod(%d, %d, &div, &mod), mod == %d instead of %d", a, b, mod, a%b) |
||||
} |
||||
i++ |
||||
} |
||||
} |
@ -0,0 +1,44 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"strings" |
||||
"testing" |
||||
|
||||
"strconv" |
||||
|
||||
"github.com/01-edu/z01" |
||||
) |
||||
|
||||
func TestDoop(t *testing.T) { |
||||
|
||||
operatorsTable := []string{"+", "-", "*", "/", "%"} |
||||
|
||||
table := []string{} |
||||
|
||||
for i := 0; i < 4; i++ { |
||||
firstArg := strconv.Itoa(z01.RandIntBetween(-1000, 1000)) |
||||
secondArg := strconv.Itoa(z01.RandIntBetween(0, 1000)) |
||||
|
||||
for _, operator := range operatorsTable { |
||||
table = append(table, firstArg+" "+operator+" "+secondArg) |
||||
|
||||
} |
||||
} |
||||
|
||||
table = append(table, "1 + 1") |
||||
table = append(table, "hello + 1") |
||||
table = append(table, "1 p 1") |
||||
table = append(table, "1 / 0") |
||||
table = append(table, "1 # 1") |
||||
table = append(table, "1 % 0") |
||||
table = append(table, "1 * 1") |
||||
table = append(table, "1argument") |
||||
table = append(table, "2 arguments") |
||||
table = append(table, "4 arguments so invalid") |
||||
table = append(table, "9223372036854775807 + 1") |
||||
table = append(table, "9223372036854775809 - 3") |
||||
table = append(table, "9223372036854775807 * 3") |
||||
for _, s := range table { |
||||
z01.ChallengeMain(t, strings.Fields(s)...) |
||||
} |
||||
} |
@ -0,0 +1,14 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
func TestEightQueens(t *testing.T) { |
||||
z01.Challenge(t, student.EightQueens, solutions.EightQueens) |
||||
} |
@ -0,0 +1,70 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
solutions "./solutions" |
||||
student "./student" |
||||
"github.com/01-edu/z01" |
||||
"testing" |
||||
) |
||||
|
||||
func TestEnigma(t *testing.T) { |
||||
args := []int{z01.RandIntBetween(2, 20)} |
||||
for i := 0; i < 3; i++ { |
||||
args = append(args, z01.RandIntBetween(2, 20)) |
||||
} |
||||
|
||||
aval := args[0] |
||||
x := args[0] |
||||
y := &x |
||||
z := &y |
||||
a := &z |
||||
|
||||
bval := args[1] |
||||
w := args[1] |
||||
b := &w |
||||
|
||||
cval := args[2] |
||||
u := args[2] |
||||
e := &u |
||||
f := &e |
||||
g := &f |
||||
h := &g |
||||
i := &h |
||||
j := &i |
||||
c := &j |
||||
|
||||
dval := args[3] |
||||
k := args[3] |
||||
l := &k |
||||
m := &l |
||||
n := &m |
||||
d := &n |
||||
|
||||
student.Enigma(a, b, c, d) |
||||
solutions.Decript(a, b, c, d) |
||||
|
||||
if aval != ***a { |
||||
t.Errorf("Expected ***a = %d instead of %d\n", |
||||
aval, |
||||
***a, |
||||
) |
||||
} |
||||
if bval != *b { |
||||
t.Errorf("Expected *b = %d instead of %d\n", |
||||
bval, |
||||
*b, |
||||
) |
||||
} |
||||
if cval != *******c { |
||||
t.Errorf("Expected *******c = %d instead of %d\n", |
||||
cval, |
||||
*******c, |
||||
) |
||||
} |
||||
if dval != ****d { |
||||
t.Errorf("Expected ****d = %d instead of %d\n", |
||||
dval, |
||||
****d, |
||||
) |
||||
} |
||||
} |
@ -0,0 +1,35 @@
|
||||
#!/bin/sh |
||||
|
||||
# Unofficial Bash Strict Mode |
||||
set -euo pipefail |
||||
IFS=' |
||||
' |
||||
|
||||
cp -rT /app /jail |
||||
|
||||
if test -v EXPECTED_FILES; then |
||||
echo -n "Formatting... " |
||||
cd /jail/student |
||||
s=$(goimports -d $(echo $EXPECTED_FILES | fmt -w1 | grep '\.go$')) |
||||
if test "$s"; then |
||||
echo |
||||
echo "$s" |
||||
exit 1 |
||||
fi |
||||
echo OK |
||||
if test -v ALLOWED_FUNCTIONS; then |
||||
rc "$EXPECTED_FILES" $ALLOWED_FUNCTIONS |
||||
fi |
||||
fi |
||||
|
||||
cd /jail |
||||
|
||||
if ! test -f ${EXERCISE}_test.go; then |
||||
echo No test file found for the exercise : "$EXERCISE" |
||||
exit 1 |
||||
fi |
||||
|
||||
# TODO: maybe change btree exercises so that they work without student code and then delete this line |
||||
find . -name '*_test.go' ! -name ${EXERCISE}_test.go -delete |
||||
|
||||
go test -failfast -run=\(?i\)test${EXERCISE} |
@ -0,0 +1,22 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
func TestFibonacci(t *testing.T) { |
||||
table := append( |
||||
z01.MultRandIntBetween(0, 25), |
||||
4, |
||||
5, |
||||
-5, |
||||
) |
||||
for _, arg := range table { |
||||
z01.Challenge(t, student.Fibonacci, solutions.Fibonacci, arg) |
||||
} |
||||
} |
@ -0,0 +1,36 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
func TestFindNextPrime(t *testing.T) { |
||||
table := append( |
||||
z01.MultRandIntBetween(-1000000, 1000000), |
||||
0, |
||||
1, |
||||
2, |
||||
3, |
||||
4, |
||||
5, |
||||
6, |
||||
7, |
||||
8, |
||||
9, |
||||
10, |
||||
11, |
||||
12, |
||||
100, |
||||
1000000086, |
||||
1000000087, |
||||
1000000088, |
||||
) |
||||
for _, arg := range table { |
||||
z01.Challenge(t, student.FindNextPrime, solutions.FindNextPrime, arg) |
||||
} |
||||
} |
@ -0,0 +1,23 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
func TestFirstRune(t *testing.T) { |
||||
table := append( |
||||
z01.MultRandASCII(), |
||||
"Hello!", |
||||
"Salut!", |
||||
"Ola!", |
||||
"♥01", |
||||
) |
||||
for _, arg := range table { |
||||
z01.Challenge(t, student.FirstRune, solutions.FirstRune, arg) |
||||
} |
||||
} |
@ -0,0 +1,12 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
) |
||||
|
||||
func TestFixthemain(t *testing.T) { |
||||
|
||||
z01.ChallengeMain(t) |
||||
} |
@ -0,0 +1,51 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
) |
||||
|
||||
type node struct { |
||||
flags []string |
||||
flagsShorthand []string |
||||
randArgFlag []string |
||||
randArg []string |
||||
} |
||||
|
||||
func TestFlags(t *testing.T) { |
||||
str := []string{"--insert=", "--order"} |
||||
strShorthand := []string{"-i=", "-o"} |
||||
var randflag []string |
||||
var randflagarg []string |
||||
for i := 0; i < 2; i++ { |
||||
randflagarg = append(randflagarg, z01.RandWords()) |
||||
randflag = append(randflag, z01.RandWords()) |
||||
} |
||||
|
||||
node := &node{ |
||||
flags: str, |
||||
flagsShorthand: strShorthand, |
||||
randArgFlag: randflagarg, |
||||
randArg: randflag, |
||||
} |
||||
|
||||
node.randArg = append(node.randArg, "") |
||||
|
||||
z01.ChallengeMain(t, node.flagsShorthand[0]+"v2", "v1") |
||||
z01.ChallengeMain(t, node.flagsShorthand[1], "v1") |
||||
z01.ChallengeMain(t, "-h") |
||||
z01.ChallengeMain(t, "--help") |
||||
z01.ChallengeMain(t) |
||||
|
||||
for _, v2 := range node.randArgFlag { |
||||
for _, v1 := range node.randArg { |
||||
z01.ChallengeMain(t, node.flags[0]+v2, node.flags[1], v1) |
||||
} |
||||
} |
||||
for _, v2 := range node.randArgFlag { |
||||
for _, v1 := range node.randArg { |
||||
z01.ChallengeMain(t, node.flagsShorthand[0]+v2, node.flagsShorthand[1], v1) |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,44 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
func TestForEach(t *testing.T) { |
||||
|
||||
functionsArray := []func(int){solutions.Add0, solutions.Add1, solutions.Add2, solutions.Add3} |
||||
|
||||
type node struct { |
||||
f func(int) |
||||
arr []int |
||||
} |
||||
|
||||
table := []node{} |
||||
|
||||
//15 random slice of random ints with a random function from selection
|
||||
|
||||
for i := 0; i < 15; i++ { |
||||
|
||||
functionSelected := functionsArray[z01.RandIntBetween(0, len(functionsArray)-1)] |
||||
val := node{ |
||||
f: functionSelected, |
||||
arr: z01.MultRandIntBetween(-1000000, 1000000), |
||||
} |
||||
table = append(table, val) |
||||
|
||||
} |
||||
|
||||
table = append(table, node{ |
||||
f: solutions.Add0, |
||||
arr: []int{1, 2, 3, 4, 5, 6}, |
||||
}) |
||||
|
||||
for _, arg := range table { |
||||
z01.Challenge(t, student.ForEach, solutions.ForEach, arg.f, arg.arr) |
||||
} |
||||
} |
@ -0,0 +1,55 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
func TestIndex(t *testing.T) { |
||||
type node struct { |
||||
s string |
||||
toFind string |
||||
} |
||||
|
||||
table := []node{} |
||||
|
||||
// the first 15 values are valid for this test
|
||||
for i := 0; i < 15; i++ { |
||||
wordToTest := z01.RandASCII() |
||||
firstLetterIndex := z01.RandIntBetween(0, (len(wordToTest)-1)/2) |
||||
lastLetterIndex := z01.RandIntBetween(firstLetterIndex, len(wordToTest)-1) |
||||
|
||||
val := node{ |
||||
s: wordToTest, |
||||
toFind: wordToTest[firstLetterIndex:lastLetterIndex], |
||||
} |
||||
table = append(table, val) |
||||
} |
||||
|
||||
// the next 15 values are supposed to be invalid for this test
|
||||
for i := 0; i < 15; i++ { |
||||
wordToTest := z01.RandASCII() |
||||
wrongMatch := z01.RandASCII() |
||||
|
||||
val := node{ |
||||
s: wordToTest, |
||||
toFind: wrongMatch, |
||||
} |
||||
table = append(table, val) |
||||
|
||||
} |
||||
// those are the test values from the README examples
|
||||
table = append(table, |
||||
node{s: "Hello!", toFind: "l"}, |
||||
node{s: "Salut!", toFind: "alu"}, |
||||
node{s: "Ola!", toFind: "hOl"}, |
||||
) |
||||
|
||||
for _, arg := range table { |
||||
z01.Challenge(t, student.Index, solutions.Index, arg.s, arg.toFind) |
||||
} |
||||
} |
@ -0,0 +1,28 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
func TestIsAlpha(t *testing.T) { |
||||
table := append( |
||||
z01.MultRandASCII(), |
||||
"Hello! €How are you?", |
||||
"a", |
||||
"z", |
||||
"!", |
||||
"HelloHowareyou", |
||||
"What's this 4?", |
||||
"Whatsthis4", |
||||
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890", |
||||
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!", |
||||
) |
||||
for _, arg := range table { |
||||
z01.Challenge(t, student.IsAlpha, solutions.IsAlpha, arg) |
||||
} |
||||
} |
@ -0,0 +1,47 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
func TestIsLower(t *testing.T) { |
||||
// 15 unvalid strings in the table
|
||||
table := z01.MultRandASCII() |
||||
|
||||
// 15 valid lowercase strings of random size between 1 and 20 letters in the table
|
||||
for i := 0; i < 15; i++ { |
||||
size := z01.RandIntBetween(1, 20) |
||||
randLow := z01.RandLower() |
||||
if len(randLow) <= size { |
||||
table = append(table, randLow) |
||||
} else { |
||||
table = append(table, randLow[:size]) |
||||
} |
||||
} |
||||
|
||||
// Special cases added to table
|
||||
table = append(table, |
||||
"Hello! How are you?", |
||||
"a", |
||||
"z", |
||||
"!", |
||||
"HelloHowareyou", |
||||
"What's this 4?", |
||||
"Whatsthis4", |
||||
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890", |
||||
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!", |
||||
"0123456789", |
||||
"01,02,03", |
||||
"abcdefghijklmnopqrstuvwxyz", |
||||
"hello", |
||||
"hello!", |
||||
) |
||||
for _, arg := range table { |
||||
z01.Challenge(t, student.IsLower, solutions.IsLower, arg) |
||||
} |
||||
} |
@ -0,0 +1,22 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
func TestIsNegative(t *testing.T) { |
||||
table := append( |
||||
z01.MultRandInt(), |
||||
z01.MinInt, |
||||
z01.MaxInt, |
||||
0, |
||||
) |
||||
for _, arg := range table { |
||||
z01.Challenge(t, student.IsNegative, solutions.IsNegative, arg) |
||||
} |
||||
} |
@ -0,0 +1,40 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"strconv" |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
func TestIsNumeric(t *testing.T) { |
||||
// 15 unvalid strings in the table
|
||||
table := z01.MultRandASCII() |
||||
|
||||
// 15 valid strings in the table
|
||||
for i := 0; i < 15; i++ { |
||||
table = append(table, strconv.Itoa(z01.RandIntBetween(0, 1000000))) |
||||
} |
||||
|
||||
// Special cases added to table
|
||||
table = append(table, |
||||
"Hello! How are you?", |
||||
"a", |
||||
"z", |
||||
"!", |
||||
"HelloHowareyou", |
||||
"What's this 4?", |
||||
"Whatsthis4", |
||||
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890", |
||||
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!", |
||||
"0123456789", |
||||
"010203", |
||||
"01,02,03", |
||||
) |
||||
for _, arg := range table { |
||||
z01.Challenge(t, student.IsNumeric, solutions.IsNumeric, arg) |
||||
} |
||||
} |
@ -0,0 +1,35 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
func TestIsPrime(t *testing.T) { |
||||
table := append( |
||||
z01.MultRandIntBetween(-1000000, 1000000), |
||||
0, |
||||
1, |
||||
2, |
||||
3, |
||||
4, |
||||
5, |
||||
6, |
||||
7, |
||||
8, |
||||
9, |
||||
10, |
||||
11, |
||||
12, |
||||
100, |
||||
1000000086, |
||||
1000000087, |
||||
) |
||||
for _, arg := range table { |
||||
z01.Challenge(t, student.IsPrime, solutions.IsPrime, arg) |
||||
} |
||||
} |
@ -0,0 +1,51 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"math/rand" |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
func TestIsPrintable(t *testing.T) { |
||||
// 15 unvalid strings in the table
|
||||
table := z01.MultRandASCII() |
||||
|
||||
// 15 valid lowercase strings of random size between 1 and 20 letters in the table
|
||||
for i := 0; i < 15; i++ { |
||||
letters := []rune("\a\b\f\r\n\v\t") |
||||
size := z01.RandIntBetween(1, 20) |
||||
r := make([]rune, size) |
||||
for i := range r { |
||||
r[i] = letters[rand.Intn(len(letters))] |
||||
} |
||||
table = append(table, string(r)) |
||||
|
||||
} |
||||
|
||||
// Special cases added to table
|
||||
table = append(table, |
||||
"Hello! How are you?", |
||||
"a", |
||||
"z", |
||||
"!", |
||||
"HelloHowareyou", |
||||
"What's this 4?", |
||||
"Whatsthis4", |
||||
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890", |
||||
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!", |
||||
"0123456789", |
||||
"01,02,03", |
||||
"abcdefghijklmnopqrstuvwxyz", |
||||
"hello", |
||||
"hello!", |
||||
"hello\n", |
||||
"\n", |
||||
) |
||||
for _, arg := range table { |
||||
z01.Challenge(t, student.IsPrintable, solutions.IsPrintable, arg) |
||||
} |
||||
} |
@ -0,0 +1,87 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"sort" |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
func TestIsSorted(t *testing.T) { |
||||
functionsArray := []func(int, int) int{solutions.IsSortedByDiff, solutions.IsSortedBy1, solutions.IsSortedBy10} |
||||
|
||||
type node struct { |
||||
f func(int, int) int |
||||
arr []int |
||||
} |
||||
|
||||
table := []node{} |
||||
|
||||
//5 unordered slices
|
||||
for i := 0; i < 5; i++ { |
||||
|
||||
functionSelected := functionsArray[z01.RandIntBetween(0, len(functionsArray)-1)] |
||||
val := node{ |
||||
f: functionSelected, |
||||
arr: z01.MultRandIntBetween(-1000000, 1000000), |
||||
} |
||||
table = append(table, val) |
||||
|
||||
} |
||||
|
||||
//5 slices ordered in ascending order
|
||||
for i := 0; i < 5; i++ { |
||||
|
||||
functionSelected := functionsArray[z01.RandIntBetween(0, len(functionsArray)-1)] |
||||
orderedArr := z01.MultRandIntBetween(-1000000, 1000000) |
||||
sort.Ints(orderedArr) |
||||
|
||||
val := node{ |
||||
f: functionSelected, |
||||
arr: orderedArr, |
||||
} |
||||
table = append(table, val) |
||||
|
||||
} |
||||
|
||||
//5 slices ordered in descending order
|
||||
for i := 0; i < 5; i++ { |
||||
|
||||
functionSelected := functionsArray[z01.RandIntBetween(0, len(functionsArray)-1)] |
||||
reverseArr := z01.MultRandIntBetween(-1000000, 1000000) |
||||
sort.Sort(sort.Reverse(sort.IntSlice(reverseArr))) |
||||
val := node{ |
||||
f: functionSelected, |
||||
arr: reverseArr, |
||||
} |
||||
table = append(table, val) |
||||
|
||||
} |
||||
|
||||
table = append(table, node{ |
||||
f: solutions.IsSortedByDiff, |
||||
arr: []int{1, 2, 3, 4, 5, 6}, |
||||
}) |
||||
|
||||
table = append(table, node{ |
||||
f: solutions.IsSortedByDiff, |
||||
arr: []int{6, 5, 4, 3, 2, 1}, |
||||
}) |
||||
|
||||
table = append(table, node{ |
||||
f: solutions.IsSortedByDiff, |
||||
arr: []int{0, 0, 0, 0, 0, 0, 0}, |
||||
}) |
||||
|
||||
table = append(table, node{ |
||||
f: solutions.IsSortedByDiff, |
||||
arr: []int{0}, |
||||
}) |
||||
|
||||
for _, arg := range table { |
||||
z01.Challenge(t, student.IsSorted, solutions.IsSorted, arg.f, arg.arr) |
||||
} |
||||
} |
@ -0,0 +1,49 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
func TestIsUpper(t *testing.T) { |
||||
// 15 unvalid strings in the table
|
||||
table := z01.MultRandASCII() |
||||
|
||||
// 15 valid lowercase strings of random size between 1 and 20 letters in the table
|
||||
for i := 0; i < 15; i++ { |
||||
size := z01.RandIntBetween(1, 20) |
||||
randLow := z01.RandLower() |
||||
if len(randLow) <= size { |
||||
table = append(table, randLow) |
||||
} else { |
||||
table = append(table, randLow[:size]) |
||||
} |
||||
} |
||||
|
||||
// Special cases added to table
|
||||
table = append(table, |
||||
"Hello! How are you?", |
||||
"a", |
||||
"z", |
||||
"!", |
||||
"HelloHowareyou", |
||||
"What's this 4?", |
||||
"Whatsthis4", |
||||
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890", |
||||
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!", |
||||
"0123456789", |
||||
"01,02,03", |
||||
"abcdefghijklmnopqrstuvwxyz", |
||||
"hello", |
||||
"hello!", |
||||
"HELLO", |
||||
"HELLO!", |
||||
) |
||||
for _, arg := range table { |
||||
z01.Challenge(t, student.IsUpper, solutions.IsUpper, arg) |
||||
} |
||||
} |
@ -0,0 +1,24 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"math/bits" |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
func TestIterativeFactorial(t *testing.T) { |
||||
table := append( |
||||
z01.MultRandInt(), |
||||
z01.IntRange(0, 12)..., |
||||
) |
||||
if bits.UintSize == 64 { |
||||
table = append(table, z01.IntRange(13, 20)...) |
||||
} |
||||
for _, arg := range table { |
||||
z01.Challenge(t, student.IterativeFactorial, solutions.IterativeFactorial, arg) |
||||
} |
||||
} |
@ -0,0 +1,22 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
func TestIterativePower(t *testing.T) { |
||||
i := 0 |
||||
for i < 30 { |
||||
nb := z01.RandIntBetween(-8, 8) |
||||
power := z01.RandIntBetween(-10, 10) |
||||
z01.Challenge(t, student.IterativePower, solutions.IterativePower, nb, power) |
||||
i++ |
||||
} |
||||
z01.Challenge(t, student.IterativePower, solutions.IterativePower, 0, 0) |
||||
z01.Challenge(t, student.IterativePower, solutions.IterativePower, 0, 1) |
||||
} |
@ -0,0 +1,16 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
"github.com/01-edu/z01" |
||||
) |
||||
|
||||
func TestItoa(t *testing.T) { |
||||
for i := 0; i < 50; i++ { |
||||
arg := z01.RandIntBetween(-2000000000, 2000000000) |
||||
z01.Challenge(t, student.Itoa, solutions.Itoa, arg) |
||||
} |
||||
} |
@ -0,0 +1,23 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
func TestItoaBase(t *testing.T) { |
||||
for i := 0; i < 30; i++ { |
||||
value := z01.RandIntBetween(-1000000, 1000000) |
||||
base := z01.RandIntBetween(2, 16) |
||||
z01.Challenge(t, student.ItoaBase, solutions.ItoaBase, value, base) |
||||
} |
||||
for i := 0; i < 5; i++ { |
||||
base := z01.RandIntBetween(2, 16) |
||||
z01.Challenge(t, student.ItoaBase, solutions.ItoaBase, z01.MaxInt, base) |
||||
z01.Challenge(t, student.ItoaBase, solutions.ItoaBase, z01.MinInt, base) |
||||
} |
||||
} |
@ -0,0 +1,26 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
solutions "./solutions" |
||||
student "./student" |
||||
"github.com/01-edu/z01" |
||||
"testing" |
||||
) |
||||
|
||||
func TestJoin(t *testing.T) { |
||||
arg1 := []string{"hello", "how", "are", "you", "doing"} |
||||
arg2 := []string{"fine", "and", "you"} |
||||
arg3 := []string{"I'm", "O.K."} |
||||
seps := []string{" ", "-", " ,", "_", "SPC", " . "} |
||||
|
||||
args := [][]string{arg1, arg2, arg3} |
||||
|
||||
for i := 0; i < 5; i++ { |
||||
//random position for the array of arguments
|
||||
posA := z01.RandIntBetween(0, len(args)-1) |
||||
//random position for the array of separators
|
||||
posS := z01.RandIntBetween(0, len(seps)-1) |
||||
|
||||
z01.Challenge(t, student.Join, solutions.Join, args[posA], seps[posS]) |
||||
} |
||||
} |
@ -0,0 +1,23 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
func TestLastRune(t *testing.T) { |
||||
table := z01.MultRandASCII() |
||||
table = append(table, |
||||
"Hello!", |
||||
"Salut!", |
||||
"Ola!", |
||||
z01.RandStr(z01.RandIntBetween(1, 15), z01.RandAlnum()), |
||||
) |
||||
for _, arg := range table { |
||||
z01.Challenge(t, student.LastRune, solutions.LastRune, arg) |
||||
} |
||||
} |
@ -0,0 +1,101 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
solution "./solutions" |
||||
student "./student" |
||||
"github.com/01-edu/z01" |
||||
) |
||||
|
||||
type Node5 = student.NodeL |
||||
type NodeS5 = solution.NodeL |
||||
|
||||
func nodePushBackList5(l *Node5, l1 *NodeS5, data interface{}) (*Node5, *NodeS5) { |
||||
n := &Node5{Data: data} |
||||
n1 := &NodeS5{Data: data} |
||||
it := l |
||||
itS := l1 |
||||
|
||||
if it == nil { |
||||
it = n |
||||
} else { |
||||
iterator := it |
||||
for iterator.Next != nil { |
||||
iterator = iterator.Next |
||||
} |
||||
iterator.Next = n |
||||
} |
||||
if itS == nil { |
||||
itS = n1 |
||||
} else { |
||||
iterator1 := itS |
||||
for iterator1.Next != nil { |
||||
iterator1 = iterator1.Next |
||||
} |
||||
iterator1.Next = n1 |
||||
} |
||||
return it, itS |
||||
} |
||||
|
||||
// compare functions that consist on inserting using the structure node
|
||||
func comparFuncNode5(solutionList *NodeS5, l *Node5, l1 *NodeS5, t *testing.T, arg int) { |
||||
if l == nil && l1 == nil { |
||||
} else if l != nil && l1 == nil { |
||||
t.Errorf("\nListAt(%s, %d) == %v instead of %v\n\n", |
||||
solution.ListToString(solutionList), arg, l, l1) |
||||
} else if l.Data != l1.Data { |
||||
t.Errorf("\nListAt(%s, %d) == %v instead of %v\n\n", |
||||
solution.ListToString(solutionList), arg, l.Data, l1.Data) |
||||
} |
||||
} |
||||
|
||||
// exercise 7
|
||||
// finds an element of a solution.ListS using a given position
|
||||
func TestListAt(t *testing.T) { |
||||
var link *Node5 |
||||
var link2 *NodeS5 |
||||
|
||||
type nodeTest struct { |
||||
data []interface{} |
||||
pos int |
||||
} |
||||
|
||||
var table []nodeTest |
||||
|
||||
for i := 0; i < 4; i++ { |
||||
val := nodeTest{ |
||||
data: solution.ConvertIntToInterface(z01.MultRandInt()), |
||||
pos: z01.RandIntBetween(1, 12), |
||||
} |
||||
table = append(table, val) |
||||
} |
||||
|
||||
for i := 0; i < 4; i++ { |
||||
val := nodeTest{ |
||||
data: solution.ConvertIntToStringface(z01.MultRandWords()), |
||||
pos: z01.RandIntBetween(1, 12), |
||||
} |
||||
table = append(table, val) |
||||
} |
||||
|
||||
table = append(table, |
||||
nodeTest{ |
||||
data: []interface{}{"I", 1, "something", 2}, |
||||
pos: z01.RandIntBetween(1, 4), |
||||
}, |
||||
) |
||||
|
||||
for _, arg := range table { |
||||
for i := 0; i < len(arg.data); i++ { |
||||
link, link2 = nodePushBackList5(link, link2, arg.data[i]) |
||||
} |
||||
|
||||
result := student.ListAt(link, arg.pos) |
||||
result2 := solution.ListAt(link2, arg.pos) |
||||
|
||||
comparFuncNode5(link2, result, result2, t, arg.pos) |
||||
link = nil |
||||
link2 = nil |
||||
} |
||||
} |
@ -0,0 +1,84 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"strconv" |
||||
"testing" |
||||
|
||||
solution "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
type Node4 = student.NodeL |
||||
type List4 = solution.List |
||||
type NodeS4 = solution.NodeL |
||||
type ListS4 = student.List |
||||
|
||||
func listToStringStu5(l *ListS4) string { |
||||
var res string |
||||
it := l.Head |
||||
for it != nil { |
||||
switch it.Data.(type) { |
||||
case int: |
||||
res += strconv.Itoa(it.Data.(int)) + "-> " |
||||
case string: |
||||
res += it.Data.(string) + "-> " |
||||
} |
||||
it = it.Next |
||||
} |
||||
res += "<nil>" |
||||
return res |
||||
} |
||||
|
||||
func listPushBackTest4(l *ListS4, l1 *List4, data interface{}) { |
||||
n := &Node4{Data: data} |
||||
n1 := &NodeS4{Data: data} |
||||
if l.Head == nil { |
||||
l.Head = n |
||||
} else { |
||||
iterator := l.Head |
||||
for iterator.Next != nil { |
||||
iterator = iterator.Next |
||||
} |
||||
iterator.Next = n |
||||
} |
||||
if l1.Head == nil { |
||||
l1.Head = n1 |
||||
} else { |
||||
iterator1 := l1.Head |
||||
for iterator1.Next != nil { |
||||
iterator1 = iterator1.Next |
||||
} |
||||
iterator1.Next = n1 |
||||
} |
||||
} |
||||
|
||||
//exercise 6
|
||||
//simply cleans the linked solution.ListS
|
||||
func TestListClear(t *testing.T) { |
||||
link := &List4{} |
||||
link2 := &ListS4{} |
||||
|
||||
table := []solution.NodeTest{} |
||||
|
||||
table = solution.ElementsToTest(table) |
||||
|
||||
table = append(table, |
||||
solution.NodeTest{ |
||||
Data: []interface{}{"I", 1, "something", 2}, |
||||
}, |
||||
) |
||||
|
||||
for _, arg := range table { |
||||
|
||||
for i := 0; i < len(arg.Data); i++ { |
||||
listPushBackTest4(link2, link, arg.Data[i]) |
||||
} |
||||
solution.ListClear(link) |
||||
student.ListClear(link2) |
||||
|
||||
if link2.Head != nil { |
||||
t.Errorf("\nstudent list:%s\nlist:%s\n\nListClear() == %v instead of %v\n\n", |
||||
listToStringStu5(link2), solution.ListToString(link.Head), link2.Head, link.Head) |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,81 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
solution "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
type Node9 = student.NodeL |
||||
type List9 = solution.List |
||||
type NodeS9 = solution.NodeL |
||||
type ListS9 = student.List |
||||
|
||||
func listPushBackTest9(l *ListS9, l1 *List9, data interface{}) { |
||||
n := &Node9{Data: data} |
||||
n1 := &NodeS9{Data: data} |
||||
if l.Head == nil { |
||||
l.Head = n |
||||
} else { |
||||
iterator := l.Head |
||||
for iterator.Next != nil { |
||||
iterator = iterator.Next |
||||
} |
||||
iterator.Next = n |
||||
} |
||||
if l1.Head == nil { |
||||
l1.Head = n1 |
||||
} else { |
||||
iterator1 := l1.Head |
||||
for iterator1.Next != nil { |
||||
iterator1 = iterator1.Next |
||||
} |
||||
iterator1.Next = n1 |
||||
} |
||||
} |
||||
|
||||
// exercise 11
|
||||
func TestListFind(t *testing.T) { |
||||
link := &List9{} |
||||
link2 := &ListS9{} |
||||
|
||||
table := []solution.NodeTest{} |
||||
table = solution.ElementsToTest(table) |
||||
table = append(table, |
||||
solution.NodeTest{ |
||||
Data: []interface{}{"hello", "hello1", "hello2", "hello3"}, |
||||
}, |
||||
) |
||||
|
||||
for _, arg := range table { |
||||
for i := 0; i < len(arg.Data); i++ { |
||||
listPushBackTest9(link2, link, arg.Data[i]) |
||||
} |
||||
if len(arg.Data) != 0 { |
||||
aux := student.ListFind(link2, arg.Data[(len(arg.Data)-1)/2], student.CompStr) |
||||
aux1 := solution.ListFind(link, arg.Data[(len(arg.Data)-1)/2], solution.CompStr) |
||||
|
||||
if aux != nil || aux1 != nil { |
||||
if *aux != *aux1 { |
||||
t.Errorf("ListFind(ref: %s) == %s instead of %s\n", arg.Data[(len(arg.Data)-1)/2], *aux, *aux1) |
||||
} |
||||
} |
||||
} |
||||
link = &List9{} |
||||
link2 = &ListS9{} |
||||
} |
||||
|
||||
for i := 0; i < len(table[0].Data); i++ { |
||||
listPushBackTest9(link2, link, table[0].Data[i]) |
||||
} |
||||
|
||||
aux := student.ListFind(link2, "lksdf", student.CompStr) |
||||
aux1 := solution.ListFind(link, "lksdf", solution.CompStr) |
||||
if aux != nil && aux1 != nil { |
||||
if *aux != *aux1 { |
||||
t.Errorf("ListFind(ref: lksdf) == %s instead of %s\n", *aux, *aux1) |
||||
} |
||||
} |
||||
|
||||
} |
@ -0,0 +1,102 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"strconv" |
||||
"testing" |
||||
|
||||
solution "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
type Node7 = student.NodeL |
||||
type List7 = solution.List |
||||
type NodeS7 = solution.NodeL |
||||
type ListS7 = student.List |
||||
|
||||
func listToStringStu8(l *ListS7) string { |
||||
var res string |
||||
it := l.Head |
||||
for it != nil { |
||||
switch it.Data.(type) { |
||||
case int: |
||||
res += strconv.Itoa(it.Data.(int)) + "-> " |
||||
case string: |
||||
res += it.Data.(string) + "-> " |
||||
} |
||||
it = it.Next |
||||
} |
||||
res += "<nil>" |
||||
return res |
||||
} |
||||
|
||||
func listPushBackTest7(l *ListS7, l1 *List7, data interface{}) { |
||||
n := &Node7{Data: data} |
||||
n1 := &NodeS7{Data: data} |
||||
if l.Head == nil { |
||||
l.Head = n |
||||
} else { |
||||
iterator := l.Head |
||||
for iterator.Next != nil { |
||||
iterator = iterator.Next |
||||
} |
||||
iterator.Next = n |
||||
} |
||||
if l1.Head == nil { |
||||
l1.Head = n1 |
||||
} else { |
||||
iterator1 := l1.Head |
||||
for iterator1.Next != nil { |
||||
iterator1 = iterator1.Next |
||||
} |
||||
iterator1.Next = n1 |
||||
} |
||||
} |
||||
|
||||
func comparFuncList7(l *List7, l1 *ListS7, t *testing.T, f func(*Node7)) { |
||||
funcName := solution.GetName(f) |
||||
for l.Head != nil || l1.Head != nil { |
||||
if (l.Head == nil && l1.Head != nil) || (l.Head != nil && l1.Head == nil) { |
||||
t.Errorf("\nstudent list: %s\nlist: %s\nfunction used: %s\n\nListForEach() == %v instead of %v\n\n", |
||||
listToStringStu8(l1), solution.ListToString(l.Head), funcName, l1.Head, l.Head) |
||||
return |
||||
} else if l.Head.Data != l1.Head.Data { |
||||
t.Errorf("\nstudent list: %s\nlist: %s\nfunction used: %s\n\nListForEach() == %v instead of %v\n\n", |
||||
listToStringStu8(l1), solution.ListToString(l.Head), funcName, l1.Head.Data, l.Head.Data) |
||||
return |
||||
} |
||||
l.Head = l.Head.Next |
||||
l1.Head = l1.Head.Next |
||||
} |
||||
} |
||||
|
||||
//exercise 9
|
||||
//applies a function to the solution.ListS
|
||||
func TestListForEach(t *testing.T) { |
||||
link := &List7{} |
||||
link2 := &ListS7{} |
||||
table := []solution.NodeTest{} |
||||
table = solution.ElementsToTest(table) |
||||
table = append(table, |
||||
solution.NodeTest{ |
||||
Data: []interface{}{"I", 1, "something", 2}, |
||||
}, |
||||
) |
||||
for _, arg := range table { |
||||
|
||||
for i := 0; i < len(arg.Data); i++ { |
||||
listPushBackTest7(link2, link, arg.Data[i]) |
||||
} |
||||
student.ListForEach(link2, student.Add2_node) |
||||
solution.ListForEach(link, solution.Add2_node) |
||||
|
||||
comparFuncList7(link, link2, t, student.Add2_node) |
||||
|
||||
student.ListForEach(link2, student.Subtract3_node) |
||||
solution.ListForEach(link, solution.Subtract3_node) |
||||
|
||||
comparFuncList7(link, link2, t, student.Subtract3_node) |
||||
|
||||
link = &List7{} |
||||
link2 = &ListS7{} |
||||
} |
||||
} |
@ -0,0 +1,151 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"strconv" |
||||
"testing" |
||||
|
||||
solution "./solutions" |
||||
student "./student" |
||||
"github.com/01-edu/z01" |
||||
) |
||||
|
||||
type Node8 = student.NodeL |
||||
type List8 = solution.List |
||||
type NodeS8 = solution.NodeL |
||||
type ListS8 = student.List |
||||
|
||||
// function to apply, in listforeachif
|
||||
func addOneS(node *NodeS8) { |
||||
data := node.Data.(int) |
||||
data++ |
||||
node.Data = interface{}(data) |
||||
} |
||||
|
||||
// function to apply, in listforeachif
|
||||
func addOne(node *Node8) { |
||||
data := node.Data.(int) |
||||
data++ |
||||
node.Data = interface{}(data) |
||||
} |
||||
|
||||
func subtract1_sol(node *NodeS8) { |
||||
data := node.Data.(int) |
||||
data-- |
||||
node.Data = interface{}(data) |
||||
} |
||||
|
||||
func subtractOne(node *Node8) { |
||||
data := node.Data.(int) |
||||
data-- |
||||
node.Data = interface{}(data) |
||||
} |
||||
|
||||
func listToStringStu7(l *ListS8) string { |
||||
var res string |
||||
it := l.Head |
||||
for it != nil { |
||||
switch it.Data.(type) { |
||||
case int: |
||||
res += strconv.Itoa(it.Data.(int)) + "-> " |
||||
case string: |
||||
res += it.Data.(string) + "-> " |
||||
} |
||||
it = it.Next |
||||
} |
||||
res += "<nil>" |
||||
return res |
||||
} |
||||
|
||||
func listPushBackTest8(l *ListS8, l1 *List8, data interface{}) { |
||||
|
||||
n := &Node8{Data: data} |
||||
n1 := &NodeS8{Data: data} |
||||
|
||||
if l.Head == nil { |
||||
l.Head = n |
||||
} else { |
||||
iterator := l.Head |
||||
for iterator.Next != nil { |
||||
iterator = iterator.Next |
||||
} |
||||
iterator.Next = n |
||||
} |
||||
|
||||
if l1.Head == nil { |
||||
l1.Head = n1 |
||||
} else { |
||||
iterator1 := l1.Head |
||||
for iterator1.Next != nil { |
||||
iterator1 = iterator1.Next |
||||
} |
||||
iterator1.Next = n1 |
||||
} |
||||
} |
||||
|
||||
func comparFuncList8(l *List8, l1 *ListS8, t *testing.T, f func(*Node8) bool, comp func(*Node8)) { |
||||
funcFName := solution.GetName(f) |
||||
funcComp := solution.GetName(comp) |
||||
for l.Head != nil || l1.Head != nil { |
||||
if (l.Head == nil && l1.Head != nil) || (l.Head != nil && l1.Head == nil) { |
||||
t.Errorf("\nstudent list:%s\nlist:%s\nfunction f used: %s\nfunction comp: %s\n\nListForEachIf() == %v instead of %v\n\n", |
||||
listToStringStu7(l1), solution.ListToString(l.Head), funcComp, funcFName, l1.Head, l.Head) |
||||
return |
||||
} else if l.Head.Data != l1.Head.Data { |
||||
t.Errorf("\nstudent list:%s\nlist:%s\nfunction f used: %s\nfunction comp: %s\n\nListForEachIf() == %v instead of %v\n\n", |
||||
listToStringStu7(l1), solution.ListToString(l.Head), funcComp, funcFName, l1.Head.Data, l.Head.Data) |
||||
return |
||||
} |
||||
l.Head = l.Head.Next |
||||
l1.Head = l1.Head.Next |
||||
} |
||||
} |
||||
|
||||
// exercise 10
|
||||
// applies a function to an element of the linked solution.ListS
|
||||
func TestListForEachIf(t *testing.T) { |
||||
link := &ListS8{} |
||||
link2 := &List8{} |
||||
|
||||
table := []solution.NodeTest{} |
||||
table = append(table, |
||||
solution.NodeTest{ |
||||
Data: []interface{}{}, |
||||
}, |
||||
) |
||||
|
||||
// just numbers/ints
|
||||
for i := 0; i < 3; i++ { |
||||
val := solution.NodeTest{ |
||||
Data: solution.ConvertIntToInterface(z01.MultRandInt()), |
||||
} |
||||
table = append(table, val) |
||||
|
||||
} |
||||
// just strings
|
||||
for i := 0; i < 3; i++ { |
||||
val := solution.NodeTest{ |
||||
Data: solution.ConvertIntToStringface(z01.MultRandWords()), |
||||
} |
||||
table = append(table, val) |
||||
} |
||||
table = append(table, |
||||
solution.NodeTest{ |
||||
Data: []interface{}{"I", 1, "something", 2}, |
||||
}, |
||||
) |
||||
for _, arg := range table { |
||||
for i := 0; i < len(arg.Data); i++ { |
||||
listPushBackTest8(link, link2, arg.Data[i]) |
||||
} |
||||
solution.ListForEachIf(link2, addOneS, solution.IsPositive_node) |
||||
student.ListForEachIf(link, addOne, student.IsPositive_node) |
||||
comparFuncList8(link2, link, t, student.IsPositive_node, addOne) |
||||
|
||||
solution.ListForEachIf(link2, subtract1_sol, solution.IsPositive_node) |
||||
student.ListForEachIf(link, subtractOne, student.IsPositive_node) |
||||
comparFuncList8(link2, link, t, student.IsPositive_node, subtractOne) |
||||
|
||||
link = &ListS8{} |
||||
link2 = &List8{} |
||||
} |
||||
} |
@ -0,0 +1,79 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"strconv" |
||||
"testing" |
||||
|
||||
solution "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
type Node3 = student.NodeL |
||||
type List3 = solution.List |
||||
type NodeS3 = solution.NodeL |
||||
type ListS3 = student.List |
||||
|
||||
func listToStringStu9(l *ListS3) string { |
||||
var res string |
||||
it := l.Head |
||||
for it != nil { |
||||
switch it.Data.(type) { |
||||
case int: |
||||
res += strconv.Itoa(it.Data.(int)) + "-> " |
||||
case string: |
||||
res += it.Data.(string) + "-> " |
||||
} |
||||
it = it.Next |
||||
} |
||||
res += "<nil>" |
||||
return res |
||||
} |
||||
|
||||
//inserts node on two lists
|
||||
func listPushBackTest3(l *ListS3, l1 *List3, data interface{}) { |
||||
n := &Node3{Data: data} |
||||
n1 := &NodeS3{Data: data} |
||||
if l.Head == nil { |
||||
l.Head = n |
||||
l.Tail = n |
||||
} else { |
||||
l.Tail.Next = n |
||||
l.Tail = n |
||||
} |
||||
if l1.Head == nil { |
||||
l1.Head = n1 |
||||
l1.Tail = n1 |
||||
} else { |
||||
l1.Tail.Next = n1 |
||||
l1.Tail = n1 |
||||
} |
||||
} |
||||
|
||||
//exercise 5
|
||||
//last element of the solution.ListS
|
||||
func TestListLast(t *testing.T) { |
||||
link := &List3{} |
||||
link2 := &ListS3{} |
||||
table := []solution.NodeTest{} |
||||
|
||||
table = solution.ElementsToTest(table) |
||||
table = append(table, |
||||
solution.NodeTest{ |
||||
Data: []interface{}{3, 2, 1}, |
||||
}, |
||||
) |
||||
for _, arg := range table { |
||||
for i := 0; i < len(arg.Data); i++ { |
||||
listPushBackTest3(link2, link, arg.Data[i]) |
||||
} |
||||
aux := solution.ListLast(link) |
||||
aux1 := student.ListLast(link2) |
||||
|
||||
if aux != aux1 { |
||||
t.Errorf("\nlist:%s\n\nListLast() == %v instead of %v\n\n", |
||||
listToStringStu9(link2), aux1, aux) |
||||
} |
||||
link = &List3{} |
||||
link2 = &ListS3{} |
||||
} |
||||
} |
@ -0,0 +1,141 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"strconv" |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
|
||||
solution "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
type Node11 = student.NodeL |
||||
type List11 = solution.List |
||||
type NodeS11 = solution.NodeL |
||||
type ListS11 = student.List |
||||
|
||||
func listToStringStu(l *ListS11) string { |
||||
var res string |
||||
it := l.Head |
||||
for it != nil { |
||||
switch it.Data.(type) { |
||||
case int: |
||||
res += strconv.Itoa(it.Data.(int)) + "->" |
||||
case string: |
||||
res += it.Data.(string) + "->" |
||||
} |
||||
it = it.Next |
||||
} |
||||
res += "<nil>" |
||||
return res |
||||
} |
||||
|
||||
func listPushBackTest11(l *ListS11, l1 *List11, data interface{}) { |
||||
n := &Node11{Data: data} |
||||
n1 := &NodeS11{Data: data} |
||||
|
||||
if l.Head == nil { |
||||
l.Head = n |
||||
} else { |
||||
iterator := l.Head |
||||
for iterator.Next != nil { |
||||
iterator = iterator.Next |
||||
} |
||||
iterator.Next = n |
||||
} |
||||
l.Tail = n |
||||
|
||||
if l1.Head == nil { |
||||
l1.Head = n1 |
||||
} else { |
||||
iterator1 := l1.Head |
||||
for iterator1.Next != nil { |
||||
iterator1 = iterator1.Next |
||||
} |
||||
iterator1.Next = n1 |
||||
} |
||||
l1.Tail = n1 |
||||
} |
||||
|
||||
func comparFuncList11(l *List11, l1 *ListS11, t *testing.T) { |
||||
for l.Head != nil || l1.Head != nil { |
||||
if (l.Head == nil && l1.Head != nil) || (l.Head != nil && l1.Head == nil) { |
||||
t.Errorf("\nstudent list:%s\nlist:%s\n\nListMerge() == %v instead of %v\n\n", |
||||
listToStringStu(l1), solution.ListToString(l.Head), l1.Head, l.Head) |
||||
return |
||||
} else if l.Head.Data != l1.Head.Data { |
||||
t.Errorf("\nstudent list:%s\nlist:%s\n\nListMerge() == %v instead of %v\n\n", |
||||
listToStringStu(l1), solution.ListToString(l.Head), l1.Head.Data, l.Head.Data) |
||||
return |
||||
} |
||||
l.Head = l.Head.Next |
||||
l1.Head = l1.Head.Next |
||||
} |
||||
} |
||||
|
||||
//exercise 14
|
||||
func TestListMerge(t *testing.T) { |
||||
link := &List11{} |
||||
linkTest := &List11{} |
||||
link2 := &ListS11{} |
||||
link2Test := &ListS11{} |
||||
|
||||
type nodeTest struct { |
||||
data []interface{} |
||||
data2 []interface{} |
||||
} |
||||
table := []nodeTest{} |
||||
// empty list
|
||||
table = append(table, |
||||
nodeTest{ |
||||
data: []interface{}{}, |
||||
data2: []interface{}{}, |
||||
}) |
||||
table = append(table, |
||||
nodeTest{ |
||||
data: solution.ConvertIntToInterface(z01.MultRandInt()), |
||||
data2: []interface{}{}, |
||||
}) |
||||
// jut ints
|
||||
for i := 0; i < 3; i++ { |
||||
val := nodeTest{ |
||||
data: solution.ConvertIntToInterface(z01.MultRandInt()), |
||||
data2: solution.ConvertIntToInterface(z01.MultRandInt()), |
||||
} |
||||
table = append(table, val) |
||||
} |
||||
// just strings
|
||||
for i := 0; i < 2; i++ { |
||||
val := nodeTest{ |
||||
data: solution.ConvertIntToStringface(z01.MultRandWords()), |
||||
data2: solution.ConvertIntToStringface(z01.MultRandWords()), |
||||
} |
||||
table = append(table, val) |
||||
} |
||||
table = append(table, |
||||
nodeTest{ |
||||
data: []interface{}{}, |
||||
data2: []interface{}{"a", 1}, |
||||
}, |
||||
) |
||||
for _, arg := range table { |
||||
for i := 0; i < len(arg.data); i++ { |
||||
listPushBackTest11(link2, link, arg.data[i]) |
||||
} |
||||
for i := 0; i < len(arg.data2); i++ { |
||||
listPushBackTest11(link2Test, linkTest, arg.data2[i]) |
||||
} |
||||
|
||||
solution.ListMerge(link, linkTest) |
||||
|
||||
student.ListMerge(link2, link2Test) |
||||
|
||||
comparFuncList11(link, link2, t) |
||||
|
||||
link = &List11{} |
||||
linkTest = &List11{} |
||||
link2 = &ListS11{} |
||||
link2Test = &ListS11{} |
||||
} |
||||
} |
@ -0,0 +1,66 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"strconv" |
||||
"testing" |
||||
|
||||
solution "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
type ListS = solution.List |
||||
type List = student.List |
||||
|
||||
func listToStringStu10(l *List) string { |
||||
var res string |
||||
it := l.Head |
||||
for it != nil { |
||||
switch it.Data.(type) { |
||||
case int: |
||||
res += strconv.Itoa(it.Data.(int)) + "-> " |
||||
case string: |
||||
res += it.Data.(string) + "-> " |
||||
} |
||||
it = it.Next |
||||
} |
||||
res += "<nil>" |
||||
return res |
||||
} |
||||
|
||||
// makes the test, compares 2 lists one from the solutions and the other from the student
|
||||
func comparFuncList(l *ListS, l1 *List, t *testing.T, data []interface{}) { |
||||
for l.Head != nil || l1.Head != nil { |
||||
if (l.Head == nil && l1.Head != nil) || (l.Head != nil && l1.Head == nil) { |
||||
t.Errorf("\ndata used: %v\nstudent list:%s\nlist:%s\n\nListPushBack()== %v instead of %v\n\n", |
||||
data, listToStringStu10(l1), solution.ListToString(l.Head), l1.Head, l.Head) |
||||
return |
||||
} else if l.Head.Data != l1.Head.Data { |
||||
t.Errorf("\ndata used: %v\nstudent list:%s\nlist:%s\n\nListPushBack()== %v instead of %v\n\n", |
||||
data, listToStringStu10(l1), solution.ListToString(l.Head), l1.Head.Data, l.Head.Data) |
||||
return |
||||
} |
||||
l.Head = l.Head.Next |
||||
l1.Head = l1.Head.Next |
||||
} |
||||
} |
||||
|
||||
// exercise 2
|
||||
func TestListPushBack(t *testing.T) { |
||||
link := &ListS{} |
||||
link2 := &List{} |
||||
|
||||
table := []solution.NodeTest{} |
||||
table = solution.ElementsToTest(table) |
||||
table = append(table, |
||||
solution.NodeTest{ |
||||
Data: []interface{}{"Hello", "man", "how are you"}, |
||||
}, |
||||
) |
||||
for _, arg := range table { |
||||
for i := 0; i < len(arg.Data); i++ { |
||||
student.ListPushBack(link2, arg.Data[i]) |
||||
solution.ListPushBack(link, arg.Data[i]) |
||||
} |
||||
comparFuncList(link, link2, t, arg.Data) |
||||
} |
||||
} |
@ -0,0 +1,68 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"strconv" |
||||
"testing" |
||||
|
||||
solution "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
type ListSa = solution.List |
||||
type Lista = student.List |
||||
|
||||
func listToStringStu11(l *Lista) string { |
||||
var res string |
||||
it := l.Head |
||||
for it != nil { |
||||
switch it.Data.(type) { |
||||
case int: |
||||
res += strconv.Itoa(it.Data.(int)) + "-> " |
||||
case string: |
||||
res += it.Data.(string) + "-> " |
||||
} |
||||
it = it.Next |
||||
} |
||||
res += "<nil>" |
||||
return res |
||||
} |
||||
|
||||
//makes the test, compares 2 lists one from the solutions and the other from the student
|
||||
func comparFuncList1(l *Lista, l1 *ListSa, t *testing.T, data []interface{}) { |
||||
for l.Head != nil || l1.Head != nil { |
||||
if (l.Head == nil && l1.Head != nil) || (l.Head != nil && l1.Head == nil) { |
||||
t.Errorf("\ndata used: %v\nstudent list:%s\nlist:%s\n\nListPushFront()== %v instead of %v\n\n", |
||||
data, listToStringStu11(l), solution.ListToString(l1.Head), l.Head, l1.Head) |
||||
return |
||||
} else if l.Head.Data != l1.Head.Data { |
||||
t.Errorf("\ndata used: %v\nstudent list:%s\nlist:%s\n\nListPushFront()== %v instead of %v\n\n", |
||||
data, listToStringStu11(l), solution.ListToString(l1.Head), l.Head, l1.Head) |
||||
return |
||||
} |
||||
l1.Head = l1.Head.Next |
||||
l.Head = l.Head.Next |
||||
} |
||||
} |
||||
|
||||
//exercise 3
|
||||
//to insert a value in the first position of the list
|
||||
func TestListPushFront(t *testing.T) { |
||||
link := &Lista{} |
||||
link2 := &ListSa{} |
||||
table := []solution.NodeTest{} |
||||
table = solution.ElementsToTest(table) |
||||
table = append(table, |
||||
solution.NodeTest{ |
||||
Data: []interface{}{"Hello", "man", "how are you"}, |
||||
}, |
||||
) |
||||
for _, arg := range table { |
||||
for i := 0; i < len(arg.Data); i++ { |
||||
student.ListPushFront(link, arg.Data[i]) |
||||
solution.ListPushFront(link2, arg.Data[i]) |
||||
} |
||||
comparFuncList1(link, link2, t, arg.Data) |
||||
link = &Lista{} |
||||
link2 = &ListSa{} |
||||
} |
||||
} |
@ -0,0 +1,109 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"strconv" |
||||
"testing" |
||||
|
||||
solution "./solutions" |
||||
student "./student" |
||||
"github.com/01-edu/z01" |
||||
) |
||||
|
||||
type Node10 = student.NodeL |
||||
type List10 = solution.List |
||||
type NodeS10 = solution.NodeL |
||||
type ListS10 = student.List |
||||
|
||||
func listToStringStu12(l *ListS10) string { |
||||
var res string |
||||
it := l.Head |
||||
for it != nil { |
||||
switch it.Data.(type) { |
||||
case int: |
||||
res += strconv.Itoa(it.Data.(int)) + "-> " |
||||
case string: |
||||
res += it.Data.(string) + "-> " |
||||
} |
||||
it = it.Next |
||||
} |
||||
res += "<nil>" |
||||
return res |
||||
} |
||||
|
||||
func listPushBackTest10(l *ListS10, l1 *List10, data interface{}) { |
||||
n := &Node10{Data: data} |
||||
n1 := &NodeS10{Data: data} |
||||
if l.Head == nil { |
||||
l.Head = n |
||||
} else { |
||||
iterator := l.Head |
||||
for iterator.Next != nil { |
||||
iterator = iterator.Next |
||||
} |
||||
iterator.Next = n |
||||
} |
||||
if l1.Head == nil { |
||||
l1.Head = n1 |
||||
} else { |
||||
iterator1 := l1.Head |
||||
for iterator1.Next != nil { |
||||
iterator1 = iterator1.Next |
||||
} |
||||
iterator1.Next = n1 |
||||
} |
||||
} |
||||
|
||||
func comparFuncList10(l *List10, l1 *ListS10, t *testing.T, data interface{}) { |
||||
for l.Head != nil || l1.Head != nil { |
||||
if (l.Head == nil && l1.Head != nil) || (l.Head != nil && l1.Head == nil) { |
||||
t.Errorf("\ndata used: %v\nstudent list:%s\nlist:%s\n\nListRemoveIf() == %v instead of %v\n\n", |
||||
data, listToStringStu12(l1), solution.ListToString(l.Head), l1.Head, l.Head) |
||||
return |
||||
} else if l.Head.Data != l1.Head.Data { |
||||
t.Errorf("\ndata used: %v\nstudent list:%s\nlist:%s\n\nListRemoveIf() == %v instead of %v\n\n", |
||||
data, listToStringStu12(l1), solution.ListToString(l.Head), l1.Head.Data, l.Head.Data) |
||||
return |
||||
} |
||||
l.Head = l.Head.Next |
||||
l1.Head = l1.Head.Next |
||||
} |
||||
} |
||||
|
||||
//exercise 13
|
||||
//removes all the elements that are equal to a value
|
||||
func TestListRemoveIf(t *testing.T) { |
||||
link := &List10{} |
||||
link2 := &ListS10{} |
||||
var index int |
||||
table := []solution.NodeTest{} |
||||
|
||||
table = solution.ElementsToTest(table) |
||||
|
||||
table = append(table, |
||||
solution.NodeTest{ |
||||
Data: []interface{}{"hello", "hello1", "hello2", "hello3"}, |
||||
}, |
||||
) |
||||
|
||||
for _, arg := range table { |
||||
for i := 0; i < len(arg.Data); i++ { |
||||
listPushBackTest10(link2, link, arg.Data[i]) |
||||
} |
||||
aux := len(arg.Data) - 1 |
||||
|
||||
index = z01.RandIntBetween(0, aux) |
||||
if link.Head != nil && link2.Head != nil { |
||||
cho := arg.Data[index] |
||||
student.ListRemoveIf(link2, cho) |
||||
solution.ListRemoveIf(link, cho) |
||||
comparFuncList10(link, link2, t, cho) |
||||
} else { |
||||
student.ListRemoveIf(link2, 1) |
||||
solution.ListRemoveIf(link, 1) |
||||
comparFuncList10(link, link2, t, 1) |
||||
} |
||||
|
||||
link = &List10{} |
||||
link2 = &ListS10{} |
||||
} |
||||
} |
@ -0,0 +1,91 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"strconv" |
||||
"testing" |
||||
|
||||
solution "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
type Node6 = student.NodeL |
||||
type List6 = solution.List |
||||
type NodeS6 = solution.NodeL |
||||
type ListS6 = student.List |
||||
|
||||
func listToStringStu13(l *ListS6) string { |
||||
var res string |
||||
it := l.Head |
||||
for it != nil { |
||||
switch it.Data.(type) { |
||||
case int: |
||||
res += strconv.Itoa(it.Data.(int)) + "-> " |
||||
case string: |
||||
res += it.Data.(string) + "-> " |
||||
} |
||||
it = it.Next |
||||
} |
||||
res += "<nil>" |
||||
return res |
||||
} |
||||
|
||||
func comparFuncList6(l *List6, l1 *ListS6, t *testing.T) { |
||||
for l.Head != nil || l1.Head != nil { |
||||
if (l.Head == nil && l1.Head != nil) || (l.Head != nil && l1.Head == nil) { |
||||
t.Errorf("\nstudent list:%s\nlist:%s\n\nListReverse() == %v instead of %v\n\n", |
||||
listToStringStu13(l1), solution.ListToString(l.Head), l1.Head, l.Head) |
||||
return |
||||
} else if l.Head.Data != l1.Head.Data { |
||||
t.Errorf("\nstudent list:%s\nlist:%s\n\nListReverse() == %v instead of %v\n\n", |
||||
listToStringStu13(l1), solution.ListToString(l.Head), l1.Head.Data, l.Head.Data) |
||||
return |
||||
} |
||||
l.Head = l.Head.Next |
||||
l1.Head = l1.Head.Next |
||||
} |
||||
} |
||||
func listPushBackTest6(l *ListS6, l1 *List6, data interface{}) { |
||||
n := &Node6{Data: data} |
||||
n1 := &NodeS6{Data: data} |
||||
if l.Head == nil { |
||||
l.Head = n |
||||
} else { |
||||
iterator := l.Head |
||||
for iterator.Next != nil { |
||||
iterator = iterator.Next |
||||
} |
||||
iterator.Next = n |
||||
} |
||||
if l1.Head == nil { |
||||
l1.Head = n1 |
||||
} else { |
||||
iterator1 := l1.Head |
||||
for iterator1.Next != nil { |
||||
iterator1 = iterator1.Next |
||||
} |
||||
iterator1.Next = n1 |
||||
} |
||||
} |
||||
|
||||
// exercise 8
|
||||
func TestListReverse(t *testing.T) { |
||||
link := &List6{} |
||||
link2 := &ListS6{} |
||||
table := []solution.NodeTest{} |
||||
table = solution.ElementsToTest(table) |
||||
table = append(table, |
||||
solution.NodeTest{ |
||||
Data: []interface{}{"I", 1, "something", 2}, |
||||
}, |
||||
) |
||||
for _, arg := range table { |
||||
for i := 0; i < len(arg.Data); i++ { |
||||
listPushBackTest6(link2, link, arg.Data[i]) |
||||
} |
||||
student.ListReverse(link2) |
||||
solution.ListReverse(link) |
||||
comparFuncList6(link, link2, t) |
||||
link = &List6{} |
||||
link2 = &ListS6{} |
||||
} |
||||
} |
@ -0,0 +1,62 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
solution "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
type Node2 = student.NodeL |
||||
type List2 = solution.List |
||||
type NodeS2 = solution.NodeL |
||||
type ListS2 = student.List |
||||
|
||||
func listPushBackTest2(l *ListS2, l1 *List2, data interface{}) { |
||||
n := &Node2{Data: data} |
||||
n1 := &NodeS2{Data: data} |
||||
if l.Head == nil { |
||||
l.Head = n |
||||
} else { |
||||
iterator := l.Head |
||||
for iterator.Next != nil { |
||||
iterator = iterator.Next |
||||
} |
||||
iterator.Next = n |
||||
} |
||||
|
||||
if l1.Head == nil { |
||||
l1.Head = n1 |
||||
} else { |
||||
iterator1 := l1.Head |
||||
for iterator1.Next != nil { |
||||
iterator1 = iterator1.Next |
||||
} |
||||
iterator1.Next = n1 |
||||
} |
||||
} |
||||
|
||||
//exercise 4
|
||||
func TestListSize(t *testing.T) { |
||||
link := &List2{} |
||||
link2 := &ListS2{} |
||||
table := []solution.NodeTest{} |
||||
table = solution.ElementsToTest(table) |
||||
table = append(table, |
||||
solution.NodeTest{ |
||||
Data: []interface{}{"Hello", "man", "how are you"}, |
||||
}, |
||||
) |
||||
for _, arg := range table { |
||||
for i := 0; i < len(arg.Data); i++ { |
||||
listPushBackTest2(link2, link, arg.Data[i]) |
||||
} |
||||
aux := solution.ListSize(link) |
||||
aux2 := student.ListSize(link2) |
||||
if aux != aux2 { |
||||
t.Errorf("ListSize(%v) == %d instead of %d\n", solution.ListToString(link.Head), aux2, aux) |
||||
} |
||||
link = &List2{} |
||||
link2 = &ListS2{} |
||||
} |
||||
} |
@ -0,0 +1,106 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"strconv" |
||||
"testing" |
||||
|
||||
solution "./solutions" |
||||
student "./student" |
||||
"github.com/01-edu/z01" |
||||
) |
||||
|
||||
type NodeI12 = student.NodeI |
||||
type NodeIS12 = solution.NodeI |
||||
|
||||
func printListStudent(n *NodeI12) string { |
||||
var res string |
||||
it := n |
||||
for it != nil { |
||||
res += strconv.Itoa(it.Data) + "-> " |
||||
it = it.Next |
||||
} |
||||
res += "<nil>" |
||||
return res |
||||
} |
||||
|
||||
func nodePushBackListInt12(l *NodeI12, l1 *NodeIS12, data int) { |
||||
n := &NodeI12{Data: data} |
||||
n1 := &NodeIS12{Data: data} |
||||
|
||||
if l == nil { |
||||
l = n |
||||
} else { |
||||
iterator := l |
||||
for iterator.Next != nil { |
||||
iterator = iterator.Next |
||||
} |
||||
iterator.Next = n |
||||
} |
||||
|
||||
if l1 == nil { |
||||
l1 = n1 |
||||
} else { |
||||
iterator1 := l1 |
||||
for iterator1.Next != nil { |
||||
iterator1 = iterator1.Next |
||||
} |
||||
iterator1.Next = n1 |
||||
} |
||||
} |
||||
|
||||
func comparFuncNodeInt12(l *NodeI12, l1 *NodeIS12, t *testing.T) { |
||||
for l != nil || l1 != nil { |
||||
if (l == nil && l1 != nil) || (l != nil && l1 == nil) { |
||||
t.Errorf("\nstudent list:%s\nlist:%s\n\nListSort() == %v instead of %v\n\n", |
||||
printListStudent(l), solution.PrintList(l1), l, l1) |
||||
return |
||||
} else if l.Data != l1.Data { |
||||
t.Errorf("\nstudent list:%s\nlist:%s\n\nListSort() == %v instead of %v\n\n", |
||||
printListStudent(l), solution.PrintList(l1), l.Data, l1.Data) |
||||
return |
||||
} |
||||
l = l.Next |
||||
l1 = l1.Next |
||||
} |
||||
} |
||||
|
||||
//exercise 15
|
||||
func TestListSort(t *testing.T) { |
||||
var link *NodeI12 |
||||
var link2 *NodeIS12 |
||||
|
||||
type nodeTest struct { |
||||
data []int |
||||
} |
||||
table := []nodeTest{} |
||||
|
||||
table = append(table, |
||||
nodeTest{ |
||||
data: []int{}, |
||||
}) |
||||
//just numbers/ints
|
||||
for i := 0; i < 2; i++ { |
||||
val := nodeTest{ |
||||
data: z01.MultRandInt(), |
||||
} |
||||
table = append(table, val) |
||||
} |
||||
|
||||
table = append(table, |
||||
nodeTest{ |
||||
data: []int{5, 4, 3, 2, 1}, |
||||
}, |
||||
) |
||||
for _, arg := range table { |
||||
for i := 0; i < len(arg.data); i++ { |
||||
nodePushBackListInt12(link, link2, arg.data[i]) |
||||
} |
||||
aux := solution.ListSort(link2) |
||||
aux2 := student.ListSort(link) |
||||
|
||||
comparFuncNodeInt12(aux2, aux, t) |
||||
|
||||
link = &NodeI12{} |
||||
link2 = &NodeIS12{} |
||||
} |
||||
} |
@ -0,0 +1,50 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
func TestMakeRange(t *testing.T) { |
||||
type node struct { |
||||
min int |
||||
max int |
||||
} |
||||
|
||||
table := []node{} |
||||
|
||||
// 15 random pairs of ints for a Valid Range
|
||||
for i := 0; i < 15; i++ { |
||||
minVal := z01.RandIntBetween(-10000000, 1000000) |
||||
gap := z01.RandIntBetween(1, 20) |
||||
val := node{ |
||||
min: minVal, |
||||
max: minVal + gap, |
||||
} |
||||
table = append(table, val) |
||||
} |
||||
// 15 random pairs of ints with ||invalid range||
|
||||
for i := 0; i < 15; i++ { |
||||
minVal := z01.RandIntBetween(-10000000, 1000000) |
||||
gap := z01.RandIntBetween(1, 20) |
||||
val := node{ |
||||
min: minVal, |
||||
max: minVal - gap, |
||||
} |
||||
table = append(table, val) |
||||
} |
||||
|
||||
table = append(table, |
||||
node{min: 0, max: 1}, |
||||
node{min: 0, max: 0}, |
||||
node{min: 5, max: 10}, |
||||
node{min: 10, max: 5}, |
||||
) |
||||
for _, arg := range table { |
||||
z01.Challenge(t, student.MakeRange, solutions.MakeRange, arg.min, arg.max) |
||||
} |
||||
} |
@ -0,0 +1,41 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
func TestMap(t *testing.T) { |
||||
functionsArray := []func(int) bool{solutions.IsPositive, solutions.IsNegative0, solutions.IsPrime} |
||||
|
||||
type node struct { |
||||
f func(int) bool |
||||
arr []int |
||||
} |
||||
|
||||
table := []node{} |
||||
|
||||
for i := 0; i < 15; i++ { |
||||
|
||||
functionSelected := functionsArray[z01.RandIntBetween(0, len(functionsArray)-1)] |
||||
val := node{ |
||||
f: functionSelected, |
||||
arr: z01.MultRandIntBetween(-1000000, 1000000), |
||||
} |
||||
table = append(table, val) |
||||
|
||||
} |
||||
|
||||
table = append(table, node{ |
||||
f: solutions.IsPrime, |
||||
arr: []int{1, 2, 3, 4, 5, 6}, |
||||
}) |
||||
|
||||
for _, arg := range table { |
||||
z01.Challenge(t, student.Map, solutions.Map, arg.f, arg.arr) |
||||
} |
||||
} |
@ -0,0 +1,18 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
solutions "./solutions" |
||||
student "./student" |
||||
"github.com/01-edu/z01" |
||||
"testing" |
||||
) |
||||
|
||||
func TestMax(t *testing.T) { |
||||
args := []int{z01.RandInt()} |
||||
limit := z01.RandIntBetween(20, 50) |
||||
for i := 0; i < limit; i++ { |
||||
args = append(args, z01.RandInt()) |
||||
} |
||||
|
||||
z01.Challenge(t, student.Max, solutions.Max, args) |
||||
} |
@ -0,0 +1,47 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"strconv" |
||||
"strings" |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
) |
||||
|
||||
func TestNbrConvertAlpha(t *testing.T) { |
||||
|
||||
type node struct { |
||||
array []string |
||||
} |
||||
|
||||
table := []node{} |
||||
for i := 0; i < 5; i++ { |
||||
m := z01.MultRandIntBetween(1, 46) |
||||
s := "" |
||||
str := []string{} |
||||
for _, j := range m { |
||||
s += strconv.Itoa(j) + " " |
||||
} |
||||
str = append(str, s) |
||||
v := node{ |
||||
array: str, |
||||
} |
||||
table = append(table, v) |
||||
} |
||||
|
||||
table = append(table, node{ |
||||
array: []string{ |
||||
"--upper 8 5 25", |
||||
"8 5 12 12 15", |
||||
"12 5 7 5 14 56 4 1 18 25", |
||||
"12 5 7 5 14 56 4 1 18 25 32 86 h", |
||||
"32 86 h", |
||||
""}, |
||||
}) |
||||
|
||||
for _, i := range table { |
||||
for _, a := range i.array { |
||||
z01.ChallengeMain(t, strings.Fields((a))...) |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,45 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"math/rand" |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
func TestNRune(t *testing.T) { |
||||
type node struct { |
||||
word string |
||||
n int |
||||
} |
||||
|
||||
table := []node{} |
||||
|
||||
for i := 0; i < 30; i++ { |
||||
wordToInput := z01.RandASCII() |
||||
val := node{ |
||||
word: wordToInput, |
||||
n: rand.Intn(len(wordToInput)) + 1, |
||||
} |
||||
table = append(table, val) |
||||
} |
||||
|
||||
table = append(table, |
||||
node{word: "Hello!", n: 3}, |
||||
node{word: "Salut!", n: 2}, |
||||
node{word: "Ola!", n: 4}, |
||||
node{word: "♥01!", n: 1}, |
||||
node{word: "Not", n: 6}, |
||||
node{word: "Also not", n: 9}, |
||||
node{word: "Tree house", n: 5}, |
||||
node{word: "Whatever", n: 0}, |
||||
node{word: "Whatshisname", n: -2}, |
||||
) |
||||
|
||||
for _, arg := range table { |
||||
z01.Challenge(t, student.NRune, solutions.NRune, arg.word, arg.n) |
||||
} |
||||
} |
@ -0,0 +1,36 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"strconv" |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
) |
||||
|
||||
func TestParamCount(t *testing.T) { |
||||
arg1 := []string{"2", "5", "u", "19"} |
||||
arg2 := []string{"2"} |
||||
arg3 := []string{"1", "2", "3", "5", "7", "24"} |
||||
arg4 := []string{"6", "12", "24"} |
||||
|
||||
args := [][]string{arg1, arg2, arg3, arg4} |
||||
|
||||
for i := 0; i < 10; i++ { |
||||
var arg []string |
||||
init := z01.RandIntBetween(0, 10) |
||||
for i := init; i < init+z01.RandIntBetween(5, 10); i++ { |
||||
arg = append(arg, strconv.Itoa(i)) |
||||
} |
||||
args = append(args, arg) |
||||
} |
||||
|
||||
for i := 0; i < 1; i++ { |
||||
args = append(args, z01.MultRandWords()) |
||||
} |
||||
|
||||
for _, v := range args { |
||||
z01.ChallengeMain(t, v...) |
||||
} |
||||
|
||||
z01.ChallengeMain(t) |
||||
} |
@ -0,0 +1,10 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"github.com/01-edu/z01" |
||||
"testing" |
||||
) |
||||
|
||||
func TestPilot(t *testing.T) { |
||||
z01.ChallengeMain(t) |
||||
} |
@ -0,0 +1,11 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
) |
||||
|
||||
func TestPoint(t *testing.T) { |
||||
z01.ChallengeMain(t, "") |
||||
} |
@ -0,0 +1,15 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
student "./student" |
||||
) |
||||
|
||||
func TestPointOne(t *testing.T) { |
||||
n := 0 |
||||
student.PointOne(&n) |
||||
if n != 1 { |
||||
t.Errorf("PointOne(&n), n == %d instead of 1", n) |
||||
} |
||||
} |
@ -0,0 +1,10 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"github.com/01-edu/z01" |
||||
"testing" |
||||
) |
||||
|
||||
func TestPrintAlphabet(t *testing.T) { |
||||
z01.ChallengeMain(t) |
||||
} |
@ -0,0 +1,14 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
func TestPrintComb2(t *testing.T) { |
||||
z01.Challenge(t, student.PrintComb2, solutions.PrintComb2) |
||||
} |
@ -0,0 +1,14 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
func TestPrintComb(t *testing.T) { |
||||
z01.Challenge(t, student.PrintComb, solutions.PrintComb) |
||||
} |
@ -0,0 +1,17 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
func TestPrintCombN(t *testing.T) { |
||||
table := []int{1, 2, 3, 4, 5, 6, 7, 8, 9} |
||||
for _, arg := range table { |
||||
z01.Challenge(t, student.PrintCombN, solutions.PrintCombN, arg) |
||||
} |
||||
} |
@ -0,0 +1,10 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"github.com/01-edu/z01" |
||||
"testing" |
||||
) |
||||
|
||||
func TestPrintDigits(t *testing.T) { |
||||
z01.ChallengeMain(t) |
||||
} |
@ -0,0 +1,22 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
func TestPrintNbr(t *testing.T) { |
||||
table := append( |
||||
z01.MultRandInt(), |
||||
z01.MinInt, |
||||
z01.MaxInt, |
||||
0, |
||||
) |
||||
for _, arg := range table { |
||||
z01.Challenge(t, student.PrintNbr, solutions.PrintNbr, arg) |
||||
} |
||||
} |
@ -0,0 +1,51 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
) |
||||
|
||||
func TestPrintNbrBase(t *testing.T) { |
||||
type node struct { |
||||
n int |
||||
base string |
||||
} |
||||
|
||||
table := []node{} |
||||
|
||||
// 15 random pairs of ints with valid bases
|
||||
for i := 0; i < 15; i++ { |
||||
validBaseToInput := solutions.RandomValidBase() |
||||
val := node{ |
||||
n: z01.RandIntBetween(-1000000, 1000000), |
||||
base: validBaseToInput, |
||||
} |
||||
table = append(table, val) |
||||
} |
||||
|
||||
// 15 random pairs of ints with invalid bases
|
||||
for i := 0; i < 15; i++ { |
||||
invalidBaseToInput := solutions.RandomInvalidBase() |
||||
val := node{ |
||||
n: z01.RandIntBetween(-1000000, 1000000), |
||||
base: invalidBaseToInput, |
||||
} |
||||
table = append(table, val) |
||||
} |
||||
|
||||
table = append(table, |
||||
node{n: 125, base: "0123456789"}, |
||||
node{n: -125, base: "01"}, |
||||
node{n: 125, base: "0123456789ABCDEF"}, |
||||
node{n: -125, base: "choumi"}, |
||||
node{n: 125, base: "-ab"}, |
||||
node{n: z01.MinInt, base: "0123456789"}, |
||||
) |
||||
for _, arg := range table { |
||||
z01.Challenge(t, student.PrintNbrBase, solutions.PrintNbrBase, arg.n, arg.base) |
||||
} |
||||
} |
@ -0,0 +1,23 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
solutions "./solutions" |
||||
student "./student" |
||||
"github.com/01-edu/z01" |
||||
) |
||||
|
||||
func TestPrintNbrInOrder(t *testing.T) { |
||||
table := append( |
||||
z01.MultRandIntBetween(0, z01.MaxInt), |
||||
z01.MaxInt, |
||||
321, |
||||
321321, |
||||
0, |
||||
) |
||||
for _, arg := range table { |
||||
z01.Challenge(t, student.PrintNbrInOrder, solutions.PrintNbrInOrder, arg) |
||||
} |
||||
|
||||
} |
@ -0,0 +1,15 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"strings" |
||||
"testing" |
||||
|
||||
"github.com/01-edu/z01" |
||||
) |
||||
|
||||
func TestPrintParams(t *testing.T) { |
||||
table := append(z01.MultRandWords(), "choumi is the best cat") |
||||
for _, s := range table { |
||||
z01.ChallengeMain(t, strings.Fields(s)...) |
||||
} |
||||
} |
@ -0,0 +1,29 @@
|
||||
package student_test |
||||
|
||||
import ( |
||||
"github.com/01-edu/z01" |
||||
"strings" |
||||
"testing" |
||||
) |
||||
|
||||
func TestPrintProgramName(t *testing.T) { |
||||
exercise := strings.ToLower( |
||||
strings.TrimPrefix(t.Name(), "Test")) |
||||
out, err1 := z01.MainOut("./student/printprogramname") |
||||
if err1 != nil { |
||||
t.Errorf(err1.Error()) |
||||
} |
||||
|
||||
correct, err2 := z01.MainOut("./solutions/printprogramname") |
||||
|
||||
if err2 != nil { |
||||
t.Errorf(err2.Error()) |
||||
} |
||||
|
||||
arrOut := strings.Split(out, "/") |
||||
ArrCor := strings.Split(correct, "/") |
||||
if ArrCor[len(ArrCor)-1] != arrOut[len(arrOut)-1] { |
||||
t.Errorf("./%s prints %q instead of %q\n", |
||||
exercise, arrOut[len(arrOut)-1], ArrCor[len(ArrCor)-1]) |
||||
} |
||||
} |
Some files were not shown because too many files changed in this diff diff.show_more
Loading…
Reference in new issue