Skip to content

Commit

Permalink
Add tests for sortLetters and main method.
Browse files Browse the repository at this point in the history
  • Loading branch information
sirishavadrevu committed Jun 30, 2019
1 parent fcef8ce commit d2781d5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 45 deletions.
15 changes: 6 additions & 9 deletions 03_letters/sirigithub/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,27 @@ import (

var out io.Writer = os.Stdout

// letters returns a map of rune literals and its frequency in the given string s
// letters returns a map of rune literals and its frequency in the given input string s
func letters(s string) map[rune]int {

charecterFreq := make(map[rune]int)
// increment the counter if the charecter exists
for _, character := range s {
charecterFreq[character]++
}
fmt.Println(charecterFreq)
return charecterFreq
}

// sortLetters returns a sorted slice of strings with elements {key}:{val} from the input map m
func sortLetters(m map[rune]int) []string {
sortedStrings := make([]string, len(m))

sortedStrings := make([]string, 0, len(m))
for key, value := range m {
str := string(key) + ":" + strconv.Itoa(value)
sortedStrings = append(sortedStrings, str)
frequency := string(key) + ":" + strconv.Itoa(value)
sortedStrings = append(sortedStrings, frequency)
}
sort.Strings(sortedStrings)

return sortedStrings
}

func main() {
fmt.Println(out, strings.Join(sortLetters(letters("dasdsadsd")), "\n"))
fmt.Fprintln(out, strings.Join(sortLetters(letters("aba")), "\n"))
}
72 changes: 36 additions & 36 deletions 03_letters/sirigithub/main_test.go
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
package main

import (
"reflect"
"bytes"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

/**
Tests to write
**/
func TestLetters(t *testing.T) {
tests := []struct {
desc string
input string
expected map[rune]int
description string
input string
expected []string
}{
{
"Simple string",
"accdceee",
map[rune]int{'a': 1, 'c': 3, 'd': 1, 'e': 3},
},
{
"String with spaces",
" 223 ",
map[rune]int{' ': 5, '2': 2, '3': 1},
},
{
"Empty String",
"",
map[rune]int{},
},
{
"Empty String",
"",
map[rune]int{},
},
{
"German Umlauts",
"äö߀’üüüöäßß",
map[rune]int{'ß': 3, 'ä': 2, 'ö': 2, 'ü': 3, '’': 1, '€': 1},
},
{description: "Simple string", input: "thisisasimplestring", expected: []string{"a:1", "e:1", "g:1", "h:1", "i:4", "l:1", "m:1", "n:1", "p:1", "r:1", "s:4", "t:2"}},

{description: "String with spaces", input: " 223 ", expected: []string{" :5", "2:2", "3:1"}},

{description: "Empty String", input: "", expected: []string{}},

{description: "German Umlauts", input: "äö߀’üüüöäßß", expected: []string{"ß:3", "ä:2", "ö:2", "ü:3", "’:1", "€:1"}},

{description: "String with Back slashes", input: "a\\c\\b", expected: []string{"\\:2", "a:1", "b:1", "c:1"}},

{description: "String with Emojis", input: "😄🐷🙈🐷🏃😄", expected: []string{"🏃:1", "🐷:2", "😄:2", "🙈:1"}},
}

for _, test := range tests {
actual := letters(test.input)
if !(reflect.DeepEqual(actual, test.expected)) {
t.Errorf("Unexpected output in main()\nexpected: %d\nactual: %d", test.expected, actual)
}
actual := sortLetters(letters(test.input))
fmt.Println(actual)
expected := test.expected
t.Run(test.description, func(t *testing.T) {
assert.Equal(t, actual, expected, "actual %v but expected %v", actual, expected)
})
}
}

func TestMainOutput(t *testing.T) {
var buf bytes.Buffer
out = &buf
main()
expected := "a:2\nb:1\n"
actual := buf.String()

if expected != actual {
t.Errorf("Unexpected output in main(). Expected = %v Actual = %v", expected, actual)
}
}

0 comments on commit d2781d5

Please sign in to comment.