Add a Copy method to options, and tests for options.

This commit is contained in:
David Anderson 2016-02-29 19:34:07 -08:00
parent e8fa791c89
commit 8175ab5f5c
2 changed files with 55 additions and 0 deletions

View File

@ -72,6 +72,15 @@ func (o Options) MarshalTo(w io.Writer) error {
return nil return nil
} }
// Copy returns a shallow copy of o.
func (o Options) Copy() Options {
ret := make(Options, len(o))
for k, v := range o {
ret[k] = v
}
return ret
}
// marshalLimited serializes o into w. If nBytes > 0, as many options // marshalLimited serializes o into w. If nBytes > 0, as many options
// as possible are packed into that many bytes, inserting padding as // as possible are packed into that many bytes, inserting padding as
// needed, and the remaining unwritten options are returned. // needed, and the remaining unwritten options are returned.

46
dhcp/options_test.go Normal file
View File

@ -0,0 +1,46 @@
package dhcp
import "testing"
func TestOptionReading(t *testing.T) {
o := Options{
1: []byte{1, 2, 3},
2: []byte{3},
3: []byte{0, 1},
}
b, ok := o.Byte(2)
if !ok {
t.Fatalf("Option 2 should be a valid byte")
}
if b != 3 {
t.Fatalf("Wanted value 3 for option 2, got %d", b)
}
b, ok = o.Byte(3)
if ok {
t.Fatalf("Option 3 shouldn't be a valid byte")
}
u, ok := o.Uint16(3)
if !ok {
t.Fatalf("Option 3 should be a valid byte")
}
if u != 1 {
t.Fatalf("Wanted value 1 for option 3, got %d", u)
}
}
func TestCopy(t *testing.T) {
o := Options{
1: []byte{2},
2: []byte{3, 4},
}
o2 := o.Copy()
delete(o2, 2)
if len(o) != 2 {
t.Fatalf("Mutating Option copy mutated the original")
}
}