1
exercism fetch go run-length-encoding

cases_test.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package encode

// Source: exercism/problem-specifications
// Commit: 503a57a run-length-encoding: Fix canonical-data.json formatting
// Problem Specifications Version: 1.0.0

// run-length encode a string
var encodeTests = []struct {
	input       string
	expected    string
	description string
}{
	{"", "", "empty string"},
	{"XYZ", "XYZ", "single characters only are encoded without count"},
	{"AABBBCCCC", "2A3B4C", "string with no single characters"},
	{"WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB", "12WB12W3B24WB", "single characters mixed with repeated characters"},
	{"  hsqq qww  ", "2 hs2q q2w2 ", "multiple whitespace mixed in string"},
	{"aabbbcccc", "2a3b4c", "lowercase characters"},
}

// run-length decode a string
var decodeTests = []struct {
	input       string
	expected    string
	description string
}{
	{"", "", "empty string"},
	{"XYZ", "XYZ", "single characters only"},
	{"2A3B4C", "AABBBCCCC", "string with no single characters"},
	{"12WB12W3B24WB", "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB", "single characters with repeated characters"},
	{"2 hs2q q2w2 ", "  hsqq qww  ", "multiple whitespace mixed in string"},
	{"2a3b4c", "aabbbcccc", "lower case string"},
}

// encode and then decode
var encodeDecodeTests = []struct {
	input       string
	expected    string
	description string
}{
	{"zzz ZZ  zZ", "zzz ZZ  zZ", "encode followed by decode gives original string"},
}

run_length_encoding_test.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package encode

import "testing"

func TestRunLengthEncode(t *testing.T) {
	for _, test := range encodeTests {
		if actual := RunLengthEncode(test.input); actual != test.expected {
			t.Errorf("FAIL %s - RunLengthEncode(%s) = %q, expected %q.",
				test.description, test.input, actual, test.expected)
		}
		t.Logf("PASS RunLengthEncode - %s", test.description)
	}
}
func TestRunLengthDecode(t *testing.T) {
	for _, test := range decodeTests {
		if actual := RunLengthDecode(test.input); actual != test.expected {
			t.Errorf("FAIL %s - RunLengthDecode(%s) = %q, expected %q.",
				test.description, test.input, actual, test.expected)
		}
		t.Logf("PASS RunLengthDecode - %s", test.description)
	}
}
func TestRunLengthEncodeDecode(t *testing.T) {
	for _, test := range encodeDecodeTests {
		if actual := RunLengthDecode(RunLengthEncode(test.input)); actual != test.expected {
			t.Errorf("FAIL %s - RunLengthDecode(RunLengthEncode(%s)) = %q, expected %q.",
				test.description, test.input, actual, test.expected)
		}
		t.Logf("PASS %s", test.description)
	}
}