diff --git a/README.md b/README.md index cb85432..97e24a7 100644 --- a/README.md +++ b/README.md @@ -377,4 +377,5 @@ problems from * [Day 379](https://github.com/vaskoz/dailycodingproblem-go/issues/764) * [Day 380](https://github.com/vaskoz/dailycodingproblem-go/issues/766) * [Day 381](https://github.com/vaskoz/dailycodingproblem-go/issues/768) +* [Day 382](https://github.com/vaskoz/dailycodingproblem-go/issues/770) diff --git a/day382/problem.go b/day382/problem.go new file mode 100644 index 0000000..b4dc188 --- /dev/null +++ b/day382/problem.go @@ -0,0 +1,16 @@ +package day382 + +import ( + "encoding/base64" + "encoding/hex" +) + +// Base64DecodeDelegate just delegates +func Base64DecodeDelegate(b64 string) string { + data, err := base64.StdEncoding.DecodeString(b64) + if err != nil { + panic("bad base64 input") + } + + return hex.EncodeToString(data) +} diff --git a/day382/problem_test.go b/day382/problem_test.go new file mode 100644 index 0000000..c321318 --- /dev/null +++ b/day382/problem_test.go @@ -0,0 +1,41 @@ +package day382 + +import "testing" + +// nolint +var testcases = []struct { + base64 string + decoded string +}{ + {"3q2+7w==", "deadbeef"}, +} + +func TestBase64DecodeDelegate(t *testing.T) { + t.Parallel() + + for _, tc := range testcases { + if res := Base64DecodeDelegate(tc.base64); res != tc.decoded { + t.Errorf("Expected %v, got %v", tc.decoded, res) + } + } +} + +func TestBase64DecodeDelegateBadInput(t *testing.T) { + t.Parallel() + + defer func() { + if err := recover(); err == nil { + t.Errorf("expected a panic for non-base64 input") + } + }() + + Base64DecodeDelegate("base64NOT[]") +} + +func BenchmarkBase64DecodeDelegate(b *testing.B) { + for i := 0; i < b.N; i++ { + for _, tc := range testcases { + Base64DecodeDelegate(tc.base64) + } + } +}