mirror of
https://github.com/danderson/netboot.git
synced 2025-10-15 09:31:29 +02:00
Add a Copy method to options, and tests for options.
This commit is contained in:
parent
e8fa791c89
commit
8175ab5f5c
@ -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
46
dhcp/options_test.go
Normal 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")
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user