|
| 1 | +// Copyright 2020 Thomas.Hoehenleitner [at] seerose.net |
| 2 | +// Use of this source code is governed by a license that can be found in the LICENSE file. |
| 3 | + |
| 4 | +// Package cgot is a helper for testing the target C-code. |
| 5 | +// Each C function gets a Go wrapper which is tested in appropriate test functions. |
| 6 | +// For some reason inside the trice_test.go an 'import "C"' is not possible. |
| 7 | +// The C-files referring to the trice sources this way avoiding code duplication. |
| 8 | +// The Go functions defined here are not exported. They are called by the Go test functions in this package. |
| 9 | +// This way the test functions are executing the trice C-code compiled with the triceConfig.h here. |
| 10 | +// Inside ./testdata this file is named cgoPackage.go where it is maintained. |
| 11 | +// The test/updateTestData.sh script copied this file under the name generated_cgoPackage.go into various |
| 12 | +// package folders, where it is used separately. |
| 13 | +package cgot |
| 14 | + |
| 15 | +// #include <stdint.h> |
| 16 | +// void TriceCheck( int n ); |
| 17 | +// void TriceTransfer( void ); |
| 18 | +// unsigned TriceOutDepth( void ); |
| 19 | +// void CgoSetTriceBuffer( uint8_t* buf ); |
| 20 | +// void CgoClearTriceBuffer( void ); |
| 21 | +// #cgo CFLAGS: -g -I../../src |
| 22 | +// #include "../../src/trice.c" |
| 23 | +// #include "../../src/trice8.c" |
| 24 | +// #include "../../src/trice16.c" |
| 25 | +// #include "../../src/trice32.c" |
| 26 | +// #include "../../src/trice64.c" |
| 27 | +// #include "../../src/triceUart.c" |
| 28 | +// #include "../../src/triceAuxiliary.c" |
| 29 | +// #include "../../src/triceDoubleBuffer.c" |
| 30 | +// #include "../../src/triceRingBuffer.c" |
| 31 | +// #include "../../src/triceStackBuffer.c" |
| 32 | +// #include "../../src/triceStaticBuffer.c" |
| 33 | +// #include "../../src/xtea.c" |
| 34 | +// #include "../../src/cobsDecode.c" |
| 35 | +// #include "../../src/cobsEncode.c" |
| 36 | +// #include "../../src/tcobsv1Decode.c" |
| 37 | +// #include "../../src/tcobsv1Encode.c" |
| 38 | +// #include "../testdata/triceCheck.c" |
| 39 | +// #include "../testdata/cgoTrice.c" |
| 40 | +import "C" |
| 41 | + |
| 42 | +import ( |
| 43 | + "bufio" |
| 44 | + "fmt" |
| 45 | + "path" |
| 46 | + "runtime" |
| 47 | + "strings" |
| 48 | + "testing" |
| 49 | + "unsafe" |
| 50 | + |
| 51 | + "github.com/rokath/trice/pkg/msg" |
| 52 | + "github.com/spf13/afero" |
| 53 | + "github.com/tj/assert" |
| 54 | +) |
| 55 | + |
| 56 | +var ( |
| 57 | + triceDir string // triceDir holds the trice directory path. |
| 58 | + testLines = -1 // testLines is the common number of tested lines in triceCheck. The value -1 is for all lines, what takes time. |
| 59 | +) |
| 60 | + |
| 61 | +// https://stackoverflow.com/questions/23847003/golang-tests-and-working-directory |
| 62 | +func init() { |
| 63 | + _, filename, _, _ := runtime.Caller(0) // filename is the test executable inside the package dir like cgo_stackBuffer_noCycle_tcobs |
| 64 | + testDir := path.Dir(filename) |
| 65 | + triceDir = path.Join(testDir, "../../") |
| 66 | + C.TriceInit() |
| 67 | +} |
| 68 | + |
| 69 | +// setTriceBuffer tells the underlying C code where to output the trice byte stream. |
| 70 | +func setTriceBuffer(o []byte) { |
| 71 | + Cout := (*C.uchar)(unsafe.Pointer(&o[0])) |
| 72 | + C.CgoSetTriceBuffer(Cout) |
| 73 | +} |
| 74 | + |
| 75 | +// triceCheck performs triceCheck C-code sequence n. |
| 76 | +func triceCheck(n int) { |
| 77 | + C.TriceCheck(C.int(n)) |
| 78 | +} |
| 79 | + |
| 80 | +// triceTransfer performs the deferred trice output. |
| 81 | +func triceTransfer() { |
| 82 | + C.TriceTransfer() |
| 83 | +} |
| 84 | + |
| 85 | +// triceOutDepth returns the actual out buffer depth. |
| 86 | +func triceOutDepth() int { |
| 87 | + return int(C.TriceOutDepth()) |
| 88 | +} |
| 89 | + |
| 90 | +// triceClearOutBuffer tells the trice kernel, that the data has been red. |
| 91 | +func triceClearOutBuffer() { |
| 92 | + C.CgoClearTriceBuffer() |
| 93 | +} |
| 94 | + |
| 95 | +// linesInFile does get the lines in a file and stores them in a string slice. |
| 96 | +func linesInFile(fh afero.File) []string { // https://www.dotnetperls.com/lines-file-go |
| 97 | + // Create new Scanner. |
| 98 | + scanner := bufio.NewScanner(fh) |
| 99 | + result := []string{} |
| 100 | + // Use Scan. |
| 101 | + for scanner.Scan() { |
| 102 | + line := scanner.Text() |
| 103 | + // Append line to result. |
| 104 | + result = append(result, line) |
| 105 | + } |
| 106 | + return result |
| 107 | +} |
| 108 | + |
| 109 | +// results contains the expected result string exps for line number line. |
| 110 | +type results struct { |
| 111 | + line int |
| 112 | + exps string |
| 113 | +} |
| 114 | + |
| 115 | +func getExpectedResults(fSys *afero.Afero, filename string) (result []results) { |
| 116 | + // get all file lines into a []string |
| 117 | + f, e := fSys.Open(filename) |
| 118 | + msg.OnErr(e) |
| 119 | + lines := linesInFile(f) |
| 120 | + |
| 121 | + for i, line := range lines { |
| 122 | + s := strings.Split(line, "//") |
| 123 | + if len(s) == 2 { // just one "//" |
| 124 | + lineEnd := s[1] |
| 125 | + subStr := "exp:" |
| 126 | + index := strings.LastIndex(lineEnd, subStr) |
| 127 | + if index >= 0 { |
| 128 | + var r results |
| 129 | + r.line = i + 1 // 1st line number is 1 and not 0 |
| 130 | + r.exps = strings.TrimSpace(lineEnd[index+len(subStr):]) |
| 131 | + result = append(result, r) |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + return |
| 136 | +} |
| 137 | + |
| 138 | +// logF is the log function type for executing the trice logging on binary log data in buffer as space separated numbers. |
| 139 | +// It uses the inside fSys specified til.json and returns the log output. |
| 140 | +type logF func(t *testing.T, fSys *afero.Afero, buffer string) string |
| 141 | + |
| 142 | +// triceLogTest creates a list of expected results from path.Join(triceDir, "./_test/testdata/triceCheck.c"). |
| 143 | +// It loops over the result list and executes for each result the compiled C-code. |
| 144 | +// It passes the received binary data as buffer to the triceLog function of type logF. |
| 145 | +// This function is test package specific defined. The file cgoPackage.go is |
| 146 | +// copied into all specific test packages and compiled there together with the |
| 147 | +// triceConfig.h, which holds the test package specific target code configuration. |
| 148 | +// limit is the count of executed test lines starting from the beginning. -1 ist for all. |
| 149 | +func triceLogTest(t *testing.T, triceLog logF, limit int) { |
| 150 | + |
| 151 | + osFSys := &afero.Afero{Fs: afero.NewOsFs()} |
| 152 | + //mmFSys := &afero.Afero{Fs: afero.NewMemMapFs()} |
| 153 | + |
| 154 | + // CopyFileIntoFSys(t, mmFSys, "til.json", osFSys, td+"./til.json") // needed for the trice log |
| 155 | + out := make([]byte, 32768) |
| 156 | + setTriceBuffer(out) |
| 157 | + |
| 158 | + result := getExpectedResults(osFSys, path.Join(triceDir, "./_test/testdata/triceCheck.c")) |
| 159 | + |
| 160 | + var count int |
| 161 | + for i, r := range result { |
| 162 | + |
| 163 | + count++ |
| 164 | + if limit >= 0 && count >= limit { |
| 165 | + return |
| 166 | + } |
| 167 | + |
| 168 | + fmt.Println(i, r) |
| 169 | + |
| 170 | + // target activity |
| 171 | + triceCheck(r.line) |
| 172 | + |
| 173 | + triceTransfer() // This is only for deferred modes needed, but direct modes contain this as empty function. |
| 174 | + |
| 175 | + length := triceOutDepth() |
| 176 | + bin := out[:length] // bin contains the binary trice data of trice message i in r.line |
| 177 | + |
| 178 | + buf := fmt.Sprint(bin) |
| 179 | + buffer := buf[1 : len(buf)-1] |
| 180 | + |
| 181 | + act := triceLog(t, osFSys, buffer) |
| 182 | + triceClearOutBuffer() |
| 183 | + |
| 184 | + assert.Equal(t, r.exps, strings.TrimSuffix(act, "\n")) |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +// triceLogTest2 works like triceLogTest but additionally expects doubled output: direct and deferred. |
| 189 | +func triceLogTest2(t *testing.T, triceLog0, triceLog1 logF, limit int) { |
| 190 | + |
| 191 | + osFSys := &afero.Afero{Fs: afero.NewOsFs()} |
| 192 | + |
| 193 | + // CopyFileIntoFSys(t, mmFSys, "til.json", osFSys, td+"./til.json") // needed for the trice log |
| 194 | + out := make([]byte, 32768) |
| 195 | + setTriceBuffer(out) |
| 196 | + |
| 197 | + result := getExpectedResults(osFSys, path.Join(triceDir, "./_test/testdata/triceCheck.c")) |
| 198 | + |
| 199 | + var count int |
| 200 | + for i, r := range result { |
| 201 | + |
| 202 | + count++ |
| 203 | + if limit >= 0 && count >= limit { |
| 204 | + return |
| 205 | + } |
| 206 | + fmt.Println(i, r) |
| 207 | + triceCheck(r.line) // target activity |
| 208 | + |
| 209 | + { // check direct output |
| 210 | + length := triceOutDepth() |
| 211 | + bin := out[:length] // bin contains the binary trice data of trice message i |
| 212 | + |
| 213 | + buf := fmt.Sprint(bin) |
| 214 | + buffer := buf[1 : len(buf)-1] |
| 215 | + |
| 216 | + act := triceLog0(t, osFSys, buffer) |
| 217 | + triceClearOutBuffer() |
| 218 | + |
| 219 | + assert.Equal(t, r.exps, strings.TrimSuffix(act, "\n")) |
| 220 | + } |
| 221 | + |
| 222 | + { // check deferred output |
| 223 | + triceTransfer() |
| 224 | + |
| 225 | + length := triceOutDepth() |
| 226 | + bin := out[:length] // bin contains the binary trice data of trice message i |
| 227 | + |
| 228 | + buf := fmt.Sprint(bin) |
| 229 | + buffer := buf[1 : len(buf)-1] |
| 230 | + |
| 231 | + act := triceLog1(t, osFSys, buffer) |
| 232 | + triceClearOutBuffer() |
| 233 | + |
| 234 | + assert.Equal(t, r.exps, strings.TrimSuffix(act, "\n")) |
| 235 | + } |
| 236 | + } |
| 237 | +} |
0 commit comments