github.com/simpleiot/simpleiot@v0.18.3/data/point_test.go (about)

     1  package data
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/binary"
     6  	"fmt"
     7  	"math"
     8  	"reflect"
     9  	"sort"
    10  	"testing"
    11  	"time"
    12  )
    13  
    14  func TestPointsSort(t *testing.T) {
    15  	now := time.Now()
    16  	p1 := Point{Time: now}
    17  	p2 := Point{Time: now.Add(time.Millisecond)}
    18  	p3 := Point{Time: now.Add(time.Millisecond * 2)}
    19  
    20  	exp := Points{p1, p2, p3}
    21  
    22  	t1 := Points{p1, p2, p3}
    23  
    24  	sort.Sort(t1)
    25  
    26  	if !reflect.DeepEqual(t1, exp) {
    27  		t.Errorf("t1 failed: t1: %v, exp: %v", t1, exp)
    28  	}
    29  
    30  	t2 := Points{p2, p3, p1}
    31  	sort.Sort(t2)
    32  
    33  	if !reflect.DeepEqual(t2, exp) {
    34  		t.Errorf("t2 failed, t2: %v, exp: %v", t2, exp)
    35  	}
    36  
    37  	t3 := Points{p1, p2, p3}
    38  	sort.Sort(t3)
    39  
    40  	if !reflect.DeepEqual(t3, exp) {
    41  		t.Errorf("t3 failed, t3: %v, exp: %v", t3, exp)
    42  	}
    43  }
    44  
    45  func TestPointCRC(t *testing.T) {
    46  	now := time.Now()
    47  	p1 := Point{Type: "pointa", Time: now}
    48  	p2 := Point{Type: "pointb", Time: now}
    49  
    50  	fmt.Printf("p1 CRC: %0x\n", p1.CRC())
    51  	fmt.Printf("p2 CRC: %0x\n", p2.CRC())
    52  
    53  	if p1.CRC() == p2.CRC() {
    54  		t.Error("CRC is weak")
    55  	}
    56  }
    57  
    58  func TestDecodeSerialHrPayload(t *testing.T) {
    59  	var buf bytes.Buffer
    60  
    61  	b := make([]byte, 16)
    62  	copy(b, []byte("voltage"))
    63  	buf.Write(b)
    64  
    65  	b = make([]byte, 16)
    66  	copy(b, []byte("AX"))
    67  	buf.Write(b)
    68  
    69  	start := time.Now()
    70  	startNs := start.UnixNano()
    71  	b = make([]byte, 8)
    72  	binary.LittleEndian.PutUint64(b, uint64(startNs))
    73  	buf.Write(b)
    74  
    75  	samp := (time.Millisecond * 50).Nanoseconds()
    76  	b = make([]byte, 4)
    77  	binary.LittleEndian.PutUint32(b, uint32(samp))
    78  	buf.Write(b)
    79  
    80  	binary.LittleEndian.PutUint32(b, math.Float32bits(10.5))
    81  	buf.Write(b)
    82  
    83  	binary.LittleEndian.PutUint32(b, math.Float32bits(1000.23))
    84  	buf.Write(b)
    85  
    86  	var pts Points
    87  
    88  	err := DecodeSerialHrPayload(buf.Bytes(), func(p Point) {
    89  		pts = append(pts, p)
    90  	})
    91  
    92  	if err != nil {
    93  		t.Fatal("Error decoding: ", err)
    94  	}
    95  
    96  	exp := Points{
    97  		{Time: start, Type: "voltage", Key: "AX", Value: 10.5},
    98  		{Time: start.Add(time.Millisecond * 50), Type: "voltage", Key: "AX", Value: 1000.23},
    99  	}
   100  
   101  	if len(exp) != len(pts) {
   102  		t.Fatal("Did not get the exp # points")
   103  	}
   104  
   105  	for i, e := range exp {
   106  		p := pts[i]
   107  
   108  		if p.Time.Sub(e.Time).Abs() > time.Nanosecond {
   109  			t.Error("Time not equal")
   110  		}
   111  		if p.Type != e.Type {
   112  			t.Error("Type not equal")
   113  		}
   114  		if p.Key != e.Key {
   115  			t.Error("Key not equal")
   116  		}
   117  	}
   118  }