Skip to content

Commit

Permalink
Merge 422796c into d138d5d
Browse files Browse the repository at this point in the history
  • Loading branch information
vaskoz committed Dec 6, 2019
2 parents d138d5d + 422796c commit 0d15fcb
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

16 changes: 16 additions & 0 deletions day382/problem.go
Original file line number Diff line number Diff line change
@@ -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)
}
41 changes: 41 additions & 0 deletions day382/problem_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}

0 comments on commit 0d15fcb

Please sign in to comment.