-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes_test.go
More file actions
156 lines (143 loc) · 3.85 KB
/
Copy pathtypes_test.go
File metadata and controls
156 lines (143 loc) · 3.85 KB
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
package ncode
import (
"errors"
"testing"
)
func TestNcodeTypeStringRoundTrip(t *testing.T) {
cases := []struct {
name string
in NcodeType
}{
{"NONE", NcodeTypeNone},
{"N3C6", NcodeTypeN3C6},
{"G3C6", NcodeTypeG3C6},
{"S1C6", NcodeTypeS1C6},
{"P1C6", NcodeTypeP1C6},
}
for _, c := range cases {
if got := c.in.String(); got != c.name {
t.Errorf("%v.String() = %q, want %q", c.in, got, c.name)
}
parsed, err := ParseNcodeType(c.name)
if err != nil {
t.Errorf("ParseNcodeType(%q) error: %v", c.name, err)
continue
}
if parsed != c.in {
t.Errorf("ParseNcodeType(%q) = %v, want %v", c.name, parsed, c.in)
}
}
}
func TestParseNcodeTypeUnknown(t *testing.T) {
if _, err := ParseNcodeType("XYZ7"); err == nil {
t.Error("ParseNcodeType(\"XYZ7\") should error")
}
}
func TestParseNcodeTypeMixedCase(t *testing.T) {
for _, s := range []string{"N3c6", "n3C6", "g3C6", "s1C6", "P1c6"} {
if _, err := ParseNcodeType(s); err != nil {
t.Errorf("ParseNcodeType(%q) unexpectedly errored: %v", s, err)
}
}
}
func TestSupportsLineMode(t *testing.T) {
cases := map[NcodeType]bool{
NcodeTypeNone: false,
NcodeTypeN3C6: true,
NcodeTypeG3C6: true,
NcodeTypeS1C6: false,
NcodeTypeP1C6: false,
}
for k, want := range cases {
if got := k.SupportsLineMode(); got != want {
t.Errorf("%v.SupportsLineMode() = %v, want %v", k, got, want)
}
}
}
func TestSupportsPostscript(t *testing.T) {
cases := map[NcodeType]bool{
NcodeTypeNone: false,
NcodeTypeN3C6: false,
NcodeTypeG3C6: false,
NcodeTypeS1C6: true,
NcodeTypeP1C6: true,
}
for k, want := range cases {
if got := k.SupportsPostscript(); got != want {
t.Errorf("%v.SupportsPostscript() = %v, want %v", k, got, want)
}
}
}
func TestTicketContains(t *testing.T) {
tk := Ticket{
Section: 3,
OwnerStart: 100, OwnerSize: 2,
BookStart: 0, BookSize: 1,
PageStart: 0, PageSize: 64,
}
if !tk.Contains(100, 0, 0) {
t.Error("expected Contains(100, 0, 0) true (start of range)")
}
if !tk.Contains(101, 0, 63) {
t.Error("expected Contains(101, 0, 63) true (end of range)")
}
if tk.Contains(102, 0, 0) {
t.Error("Contains(102, 0, 0) should be false (owner past end)")
}
if tk.Contains(99, 0, 0) {
t.Error("Contains(99, 0, 0) should be false (owner before start)")
}
if tk.Contains(100, 1, 0) {
t.Error("Contains(100, 1, 0) should be false (book past end)")
}
if tk.Contains(100, 0, 64) {
t.Error("Contains(100, 0, 64) should be false (page == start+size)")
}
}
func TestTicketResolve(t *testing.T) {
tk := Ticket{
Section: 3,
OwnerStart: 100, OwnerSize: 2,
BookStart: 5, BookSize: 1,
PageStart: 10, PageSize: 4,
}
id, err := tk.Resolve(0, 0, 0)
if err != nil {
t.Fatalf("Resolve(0,0,0) error: %v", err)
}
if id != (PageID{Section: 3, Owner: 100, Book: 5, Page: 10}) {
t.Errorf("Resolve(0,0,0) = %v, want section=3 owner=100 book=5 page=10", id)
}
id, err = tk.Resolve(1, 0, 3)
if err != nil {
t.Fatalf("Resolve(1,0,3) error: %v", err)
}
if id.Owner != 101 || id.Page != 13 {
t.Errorf("Resolve(1,0,3) = %v, want owner=101 page=13", id)
}
for _, bad := range [][3]int{
{-1, 0, 0},
{2, 0, 0}, // ownerOffset == OwnerSize
{0, 1, 0}, // bookOffset == BookSize
{0, 0, 4}, // pageOffset == PageSize
{0, 0, -1}, // negative
} {
if _, err := tk.Resolve(bad[0], bad[1], bad[2]); !errors.Is(err, ErrOffsetOutOfRange) {
t.Errorf("Resolve%v err = %v, want ErrOffsetOutOfRange", bad, err)
}
}
}
func TestPageIDString(t *testing.T) {
id := PageID{Section: 3, Owner: 100, Book: 5, Page: 10}
if got, want := id.String(), "3.100.5.10"; got != want {
t.Errorf("String() = %q, want %q", got, want)
}
}
func TestDotModeString(t *testing.T) {
if DotModeDot.String() != "dot" {
t.Errorf("DotModeDot.String() = %q", DotModeDot.String())
}
if DotModeLine.String() != "line" {
t.Errorf("DotModeLine.String() = %q", DotModeLine.String())
}
}