summaryrefslogtreecommitdiff
path: root/software/communication/data_link_test.go
blob: bbe9539f13e47c53246ffae502da8dd5a2e34847 (plain)
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
package communication

import (
	"reflect"
	"testing"
)

func TestUnslip(t *testing.T) {
	input := []byte{
		0xFF, 0x12, SLIP_END,
		0xFF, SLIP_ESC, SLIP_ESC_END, 0x12, SLIP_END,
		0xFF, SLIP_ESC, SLIP_ESC_ESC, 0x12, SLIP_END,
	}

	output := [][]byte{
		{0xFF, 0x12},
		{0xFF, SLIP_END, 0x12},
		{0xFF, SLIP_ESC, 0x12},
	}

	bytes := make(chan byte)
	frames := make(chan []byte)

	go func() {
		for _, v := range input {
			bytes <- v
		}
	}()

	go func() {
		for {
			frames <- unslip(bytes)
		}
	}()

	for _, v := range output {
		frame := <-frames
		if !reflect.DeepEqual(frame, v) {
			t.Fatalf("Frame '%v' does not match expected '%v'\n", frame, v);
		}
	}
}