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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
package asciiset
import (
"strings"
"testing"
)
func TestMakeASCIISet(t *testing.T) {
if _, ok := MakeASCIISet("@J"); !ok {
t.Errorf(`MakeASCIISet should identify "@J" as all ASCII`)
}
if _, ok := MakeASCIISet("@\u00f8"); ok {
t.Errorf(`MakeASCIISet should identify "@\u00f8" as not all ASCII`)
}
}
func TestAdd(t *testing.T) {
chars := "@J"
var as ASCIISet
for _, c := range chars {
as.Add(byte(c))
if !as.Contains(byte(c)) {
t.Errorf("ASCIISet should contain %s", string(c))
}
}
britishPound := byte('£')
as.Add(britishPound)
if as.Contains(britishPound) {
t.Errorf("ASCIISet should not contain %s", string(britishPound))
}
}
func TestContains(t *testing.T) {
as, _ := MakeASCIISet("@J")
if as.Contains('5') {
t.Errorf("ASCIISet should not contain 5")
}
}
func TestRemove(t *testing.T) {
chars := "@J"
as, _ := MakeASCIISet(chars)
as.Remove('@')
if as.Contains('@') {
t.Errorf("ASCIISet should not contain @")
}
if !as.Contains('J') {
t.Errorf("ASCIISet should contain J")
}
}
func TestSize(t *testing.T) {
as, _ := MakeASCIISet("ABCD")
if size := as.Size(); size != 4 {
t.Errorf("Expected Size 4, got %d", size)
}
}
func TestUnion(t *testing.T) {
as, _ := MakeASCIISet("ABCD")
as2, _ := MakeASCIISet("CDEF")
as3 := as.Union(as2)
for _, c := range "ABCDEF" {
if !as3.Contains(byte(c)) {
t.Errorf("ASCIISet should contain %s", string(c))
}
}
}
func TestIntersection(t *testing.T) {
as, _ := MakeASCIISet("ABCD")
as2, _ := MakeASCIISet("CDEF")
as3 := as.Intersection(as2)
for _, c := range "CD" {
if !as3.Contains(byte(c)) {
t.Errorf("ASCIISet should contain %s", string(c))
}
}
for _, c := range "ABEF" {
if as3.Contains(byte(c)) {
t.Errorf("ASCIISet should not contain %s", string(c))
}
}
}
func TestSubtract(t *testing.T) {
as, _ := MakeASCIISet("ABCD")
as2, _ := MakeASCIISet("CDEF")
as3 := as.Subtract(as2)
for _, c := range "AB" {
if !as3.Contains(byte(c)) {
t.Errorf("ASCIISet should contain %s", string(c))
}
}
for _, c := range "CDEF" {
if as3.Contains(byte(c)) {
t.Errorf("ASCIISet should not contain %s", string(c))
}
}
}
func TestEquals(t *testing.T) {
as, _ := MakeASCIISet("ABCD")
as2, _ := MakeASCIISet("ABCD")
if !as.Equals(as2) {
t.Errorf("as should be equal to as2")
}
}
func TestVisit(t *testing.T) {
// chars must be all unique and in ascending order
chars := "123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ"
as, _ := MakeASCIISet(chars)
// scenario: visit every character in the set
output := make([]byte, 0, len(chars))
as.Visit(func(n byte) bool {
if as.Contains(n) {
output = append(output, n)
}
return false
})
if len(output) != len(chars) {
t.Errorf("output length must be %d; visit every character. Got %d", len(chars), len(output))
}
for i := 0; i < len(chars); i++ {
if output[i] != byte(chars[i]) {
t.Errorf("%d %d", output[i], byte(chars[i]))
}
}
// scenario: stop early at 'T'
output = make([]byte, 0, strings.Index(chars, "T")+1)
as.Visit(func(n byte) bool {
if as.Contains(n) {
output = append(output, n)
}
return n == 'T'
})
if len(output) != strings.Index(chars, "T")+1 {
t.Errorf("output length must be %d; stop early at 'T'. Got %d", strings.Index(chars, "T")+1, len(output))
}
for i := 0; i < strings.Index(chars, "T")+1; i++ {
if output[i] != byte(chars[i]) {
t.Errorf("%d %d", output[i], byte(chars[i]))
}
}
// scenario: Add extra '\n'
output = make([]byte, 0, len(chars))
as.Visit(func(n byte) bool {
as.Add('\n')
if as.Contains(n) {
output = append(output, n)
}
return false
})
if as.Size() != len(chars)+1 {
t.Errorf("as.Size() must be %d; visit every character and add extra '\n'. Got %d", len(chars)+1, as.Size())
}
if len(output) != len(chars) {
t.Errorf("output length must be %d; visit every character and add extra '\n'. Got %d", len(chars), len(output))
}
for i := 0; i < len(chars); i++ {
if output[i] != byte(chars[i]) {
t.Errorf("%d %d", output[i], byte(chars[i]))
}
}
// scenario: Remove extra '\n'
output = make([]byte, 0, len(chars))
as.Visit(func(n byte) bool {
as.Remove('\n')
if as.Contains(n) {
output = append(output, n)
}
return false
})
if as.Size() != len(chars) {
t.Errorf("as.Size() must be %d; visit every character and remove extra '\n'. Got %d", len(chars), as.Size())
}
if len(output) != len(chars) {
t.Errorf("output length must be %d; visit every character and remove extra '\n'. Got %d", len(chars), len(output))
}
for i := 0; i < len(chars); i++ {
if output[i] != byte(chars[i]) {
t.Errorf("%d %d", output[i], byte(chars[i]))
}
}
}
|