From d30c9704a6d7c2aec01d3e65d5185506f328cea7 Mon Sep 17 00:00:00 2001 From: alexchao26 Date: Mon, 21 Dec 2020 19:46:50 -0500 Subject: [PATCH] casting package started, this feels strange in Go... --- cast/cast.go | 74 +++++++++++++++++++++++++++++++++++++ cast/cast_test.go | 94 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 168 insertions(+) create mode 100644 cast/cast.go create mode 100644 cast/cast_test.go diff --git a/cast/cast.go b/cast/cast.go new file mode 100644 index 0000000..324e0fa --- /dev/null +++ b/cast/cast.go @@ -0,0 +1,74 @@ +package cast + +// Suite of casting functions to speed up solutions +// This is NOT idiomatic Go... but AOC isn't about that... + +import "strconv" + +// ToInt will case a given arg into an int type. +// Supported types are: +// - string +func ToInt(arg interface{}) int { + var val int + switch arg.(type) { + case string: + var err error + val, err = strconv.Atoi(arg.(string)) + if err != nil { + panic("error converting string to int " + err.Error()) + } + default: + panic("unhandled type for int casting") + } + return val +} + +// ToString will case a given arg into an int type. +// Supported types are: +// - int +// - byte +// - rune +func ToString(arg interface{}) string { + var str string + switch arg.(type) { + case int: + str = strconv.Itoa(arg.(int)) + case byte: + b := arg.(byte) + str = string(rune(b)) + case rune: + str = string(arg.(rune)) + default: + panic("unhandled type for string casting") + } + return str +} + +const ( + ASCIICodeCapA = int('A') // 65 + ASCIICodeLowerA = int('a') // 97 +) + +// ToASCIICode returns the ascii code of a given input +func ToASCIICode(arg interface{}) int { + var asciiVal int + switch arg.(type) { + case string: + str := arg.(string) + if len(str) != 1 { + panic("can only convert ascii Code for string of length 1") + } + asciiVal = int(str[0]) + case byte: + asciiVal = int(arg.(byte)) + case rune: + asciiVal = int(arg.(rune)) + } + + return asciiVal +} + +// ASCIIIntToChar returns a one character string of the given int +func ASCIIIntToChar(code int) string { + return string(rune(code)) +} diff --git a/cast/cast_test.go b/cast/cast_test.go new file mode 100644 index 0000000..5028fb1 --- /dev/null +++ b/cast/cast_test.go @@ -0,0 +1,94 @@ +package cast_test + +import ( + "testing" + + "github.com/alexchao26/advent-of-code-go/cast" +) + +func TestToInt(t *testing.T) { + if got := cast.ToInt("123"); got != 123 { + t.Errorf("cast.Int(123) = %v, want 123", got) + } + if got := cast.ToInt("9835"); got != 9835 { + t.Errorf("cast.Int(9835) = %v, want 9835", got) + } +} + +func TestToString(t *testing.T) { + byteTests := []struct { + name string + input interface{} + want string + }{ + {"byte", byte('a'), "a"}, + {"byte", byte('x'), "x"}, + {"int", 1234, "1234"}, + {"int", 512, "512"}, + {"rune", rune(65), "A"}, + {"rune", rune(97), "a"}, + } + for _, tt := range byteTests { + t.Run(tt.name, func(t *testing.T) { + if got := cast.ToString(tt.input); got != tt.want { + t.Errorf("ToString(byte) = %q, want %q", got, tt.want) + } + }) + } +} + +func TestToASCIIConstants(t *testing.T) { + if cast.ASCIICodeCapA != 65 { + t.Errorf("Expected cast.ASCIICodeCapA to be 65, got %d", cast.ASCIICodeCapA) + } + if cast.ASCIICodeLowerA != 97 { + t.Errorf("Expected cast.ASCIICodeLowerA to be 97, got %d", cast.ASCIICodeLowerA) + } +} + +func TestToASCIICode(t *testing.T) { + type args struct { + arg interface{} + } + tests := []struct { + name string + args args + want int + }{ + {"example_string", args{"a"}, cast.ASCIICodeLowerA}, + {"example_string", args{"b"}, cast.ASCIICodeLowerA + 1}, + {"example_string", args{"z"}, cast.ASCIICodeLowerA + 25}, + {"example_string", args{"C"}, cast.ASCIICodeCapA + 2}, + {"example_rune", args{rune(97)}, 97}, + {"example_byte", args{'a'}, 97}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := cast.ToASCIICode(tt.args.arg); got != tt.want { + t.Errorf("ToASCIICode() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestASCIIIntToChar(t *testing.T) { + type args struct { + code int + } + tests := []struct { + name string + args args + want string + }{ + {"example", args{97}, "a"}, + {"example", args{98}, "b"}, + {"example", args{65}, "A"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := cast.ASCIIIntToChar(tt.args.code); got != tt.want { + t.Errorf("ASCIIIntToChar() = %v, want %v", got, tt.want) + } + }) + } +}