TDD Workshop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package myrange | |
import "fmt" | |
type RangeType int | |
const ( | |
OPEN RangeType = iota | |
CLOSE | |
OPEN_CLOSE | |
CLOSE_OPEN | |
) | |
type Range struct { | |
LowerEndpoint int | |
UpperEndpoint int | |
RangeType RangeType | |
} | |
func NewRange(lower int, upper int, rangeType RangeType) (*Range, error) { | |
if rangeType == CLOSE && lower > upper { | |
return nil, fmt.Errorf("rangeType == CLOSE && lower > upper") | |
} | |
if rangeType == OPEN && lower >= upper { | |
return nil, fmt.Errorf("rangeType == OPEN && lower >= upper") | |
} | |
return &Range{lower, upper, rangeType}, nil | |
} | |
func (r *Range) Include(in *Range) bool { | |
if r.LowerEndpoint <= in.LowerEndpoint && r.UpperEndpoint >= in.UpperEndpoint { | |
return true | |
} | |
return false | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package myrange | |
import "testing" | |
var propertyTest = []struct { | |
lower int | |
upper int | |
rangeType RangeType | |
out *Range | |
}{ | |
{1, 2, OPEN, &Range{1, 2, OPEN}}, | |
{2, 2, OPEN, nil}, | |
{2, 1, OPEN, nil}, | |
{1, 2, CLOSE, &Range{1, 2, CLOSE}}, | |
{2, 2, CLOSE, &Range{2, 2, CLOSE}}, | |
{2, 1, CLOSE, nil}, | |
} | |
func TestNewRange(t *testing.T) { | |
for _, tt := range propertyTest { | |
r, _ := NewRange(tt.lower, tt.upper, tt.rangeType) | |
if tt.out == nil { | |
if r != nil { | |
t.Errorf("expect:%v result:%v", tt, r) | |
} | |
} else { | |
if *r != *tt.out { | |
t.Errorf("expect:%v result:%v", tt, r) | |
} | |
} | |
} | |
} | |
var includeTest = []struct { | |
in1 *Range | |
in2 *Range | |
out bool | |
}{ | |
{&Range{1, 10, CLOSE}, &Range{2, 9, CLOSE}, true}, | |
{&Range{1, 10, CLOSE}, &Range{1, 10, CLOSE}, true}, | |
{&Range{1, 10, CLOSE}, &Range{2, 11, CLOSE}, false}, | |
{&Range{1, 10, CLOSE}, &Range{0, 9, CLOSE}, false}, | |
} | |
func TestInclude(t *testing.T) { | |
for _, tt := range includeTest { | |
if tt.in1.Include(tt.in2) != tt.out { | |
t.Errorf("エラー") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
cf.