tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/dht/device_internal_test.go (about) 1 package dht 2 3 import ( 4 "testing" 5 ) 6 7 func TestDeviceType_extractData(t *testing.T) { 8 bitStr := "0000001010001100000000010101111111101110" 9 buf := bitStringToBytes(bitStr) 10 11 tt := []struct { 12 name string 13 d DeviceType 14 buf []byte 15 wantTemp int16 16 wantHum uint16 17 }{ 18 { 19 // temp = 35.1C hum = 65.2% 20 name: "DHT22", d: DHT22, buf: buf, wantTemp: 351, wantHum: 652, 21 }, 22 } 23 24 for _, tc := range tt { 25 t.Run(tc.name, func(t *testing.T) { 26 gotTemp, gotHum := tc.d.extractData(tc.buf) 27 if gotTemp != tc.wantTemp { 28 t.Errorf("extractData() gotTemp = %v, want %v", gotTemp, tc.wantTemp) 29 } 30 if gotHum != tc.wantHum { 31 t.Errorf("extractData() gotHum = %v, want %v", gotHum, tc.wantHum) 32 } 33 }) 34 } 35 } 36 37 func bitStringToBytes(s string) []byte { 38 b := make([]byte, (len(s)+(8-1))/8) 39 for i, r := range s { 40 if r < '0' || r > '1' { 41 panic("not in range") 42 } 43 b[i>>3] |= byte(r-'0') << uint(7-i&7) 44 } 45 return b 46 }