FileRecord improvement: unit test for the rescue program

pull/228/head
f4exb 2018-10-09 18:28:59 +02:00
rodzic 38aa1a8e77
commit bfb7583544
3 zmienionych plików z 90 dodań i 60 usunięć

Wyświetl plik

@ -55,5 +55,11 @@ You will usually find a `golang` package in your distribution. For example in Ub
In this directory just do `go build`
<h3>Unit testing</h3>
Unit test (very simple) is located in `rescuesdriq_test.go`. It uses the [Go Convey](https://github.com/smartystreets/goconvey) framework. You should first install it with:
`go get github.com/smartystreets/goconvey`
You can run unit test from command line with: `go test`
Or with the Go Convey server that you start from this directory with: `$GOPATH/bin/goconvey` where `$GOPATH` is the path to your go installation.

Wyświetl plik

@ -1,18 +1,17 @@
package main
import (
"flag"
"fmt"
"bufio"
"io"
"os"
"bytes"
"encoding/binary"
"time"
"flag"
"fmt"
"hash/crc32"
"io"
"os"
"time"
)
type HeaderStd struct {
SampleRate uint32
CenterFrequency uint64
@ -22,7 +21,6 @@ type HeaderStd struct {
CRC32 uint32
}
func check(e error) {
if e != nil {
panic(e)
@ -35,7 +33,7 @@ func analyze(r *bufio.Reader) HeaderStd {
if err != nil && err != io.EOF {
panic(err)
}
if (n != 32) {
if n != 32 {
panic("Header too small")
}
@ -62,7 +60,7 @@ func setCRC(header *HeaderStd) {
header.CRC32 = crc32.ChecksumIEEE(bin_buf.Bytes()[0:28])
}
func getCRC(header *HeaderStd) uint32 {
func GetCRC(header *HeaderStd) uint32 {
var bin_buf bytes.Buffer
header.Filler = 0
binary.Write(&bin_buf, binary.LittleEndian, header)
@ -76,7 +74,7 @@ func printHeader(header *HeaderStd) {
tm := time.Unix(header.StartTimestamp, 0)
fmt.Println("Start :", tm)
fmt.Println("CRC32 :", header.CRC32)
fmt.Println("CRC32 OK :", getCRC(header))
fmt.Println("CRC32 OK :", GetCRC(header))
}
func copyContent(reader *bufio.Reader, writer *bufio.Writer, blockSize uint) {
@ -128,7 +126,7 @@ func main() {
check(err)
// close fi on exit and check for its returned error
defer func() {
err := fi.Close();
err := fi.Close()
check(err)
}()
// make a read buffer
@ -150,7 +148,7 @@ func main() {
}
} else if flagSeen["ts"] {
t, err := time.Parse(time.RFC3339, *timeStr)
if (err == nil) {
if err == nil {
headerOrigin.StartTimestamp = t.Unix()
} else {
fmt.Println("Incorrect time specified. Defaulting to now")
@ -170,7 +168,7 @@ func main() {
check(err)
defer func() {
err := fo.Close();
err := fo.Close()
check(err)
}()

Wyświetl plik

@ -0,0 +1,26 @@
package main
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestSpec(t *testing.T) {
// Only pass t into top-level Convey calls
Convey("Given a header structure", t, func() {
var header HeaderStd
header.SampleRate = 75000
header.CenterFrequency = 435000000
header.StartTimestamp = 1539083921
header.SampleSize = 16
header.Filler = 0
crc32 := GetCRC(&header)
Convey("The CRC32 value should be 2294957931", func() {
So(crc32, ShouldEqual, 2294957931)
})
})
}