Skip to content

Commit 4cc18f3

Browse files
committed
special test for debugging added
1 parent 493709f commit 4cc18f3

7 files changed

Lines changed: 393 additions & 0 deletions

File tree

_test/special_for_debug/ReadMe.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Attention
2+
3+
* Do **not** edit `generated_cgoPackage.go`. Change instead file `../testdata/cgoPackage.go` and execute `../updateTestData.sh` afterwards. This influences _all_ cgot packages tests.
4+
* For individual modifications use file `cgo_test.go` or create an additional file.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include "trice.h"
2+
3+
char* TargetActivity(void) {
4+
uint8_t buf[3] = {0xaa,0xbb,0xcc};
5+
TRice8B("%3x\n", buf, 3);
6+
TRice8B("%3x\n", buf, 3);
7+
return "feed3322 aa bb cc\nfeed3322 aa bb cc";
8+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package cgot
2+
3+
// For some reason inside the trice_test.go an 'import "C"' is not possible.
4+
5+
// char* TargetActivity( void );
6+
import "C"
7+
8+
func targetActivity() (r string) {
9+
return C.GoString(C.TargetActivity())
10+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package cgot
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"io"
7+
"path"
8+
"strings"
9+
"testing"
10+
11+
"github.com/rokath/trice/internal/args"
12+
"github.com/spf13/afero"
13+
"github.com/tj/assert"
14+
)
15+
16+
func TestLogs(t *testing.T) {
17+
// triceLog is the log function for executing the trice logging on binary log data in buffer as space separated numbers.
18+
// It uses the inside fSys specified til.json and returns the log output.
19+
triceLog := func(t *testing.T, fSys *afero.Afero, buffer string) string {
20+
var o bytes.Buffer
21+
assert.Nil(t, args.Handler(io.Writer(&o), fSys, []string{"trice", "log", "-i", path.Join(triceDir, "/_test/testdata/til.json"), "-p=BUFFER", "-args", buffer, "-hs=off", "-prefix=off", "-li=off", "-color=none", "-ts0", "time: ", "-ts16", "time: %04x", "-ts32", "time:%08x"}))
22+
return o.String()
23+
}
24+
triceLogSpecialTest(t, triceLog)
25+
}
26+
27+
// triceLogSpecialTest ...
28+
func triceLogSpecialTest(t *testing.T, triceLog logF) {
29+
osFSys := &afero.Afero{Fs: afero.NewOsFs()}
30+
out := make([]byte, 32768)
31+
setTriceBuffer(out)
32+
33+
exp := targetActivity()
34+
35+
// For the ring buffer, we need to call triceTransfer() at least for each Trice statement in targetActivity().
36+
// For the double buffer one call is enough.
37+
for i := 0; i < 5; i++ {
38+
triceTransfer()
39+
}
40+
41+
length := triceOutDepth()
42+
bin := out[:length] // bin contains the binary trice data of trice message.
43+
44+
buf := fmt.Sprint(bin)
45+
buffer := buf[1 : len(buf)-1]
46+
47+
act := triceLog(t, osFSys, buffer) // convert binary buffer into result string
48+
49+
triceClearOutBuffer()
50+
51+
assert.Equal(t, exp, strings.TrimSuffix(act, "\n"))
52+
53+
}
Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
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+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*! \file triceConfig.h
2+
\author Thomas.Hoehenleitner [at] seerose.net
3+
*******************************************************************************/
4+
5+
#ifndef TRICE_CONFIG_H_
6+
#define TRICE_CONFIG_H_
7+
8+
#ifdef __cplusplus
9+
extern "C" {
10+
#endif
11+
12+
#define TRICE_CLEAN 1
13+
14+
#define TRICE_BUFFER TRICE_DOUBLE_BUFFER
15+
16+
#define TRICE_DEFERRED_BUFFER_SIZE 512
17+
18+
#define TRICE_DEFERRED_OUTPUT 1
19+
#define TRICE_DEFERRED_UARTA 1
20+
#define TRICE_UARTA
21+
22+
#define TRICE_CGO 1
23+
#define TRICE_CYCLE_COUNTER 0
24+
25+
#ifdef __cplusplus
26+
}
27+
#endif
28+
29+
#endif /* TRICE_CONFIG_H_ */
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*! \file triceUart.h
2+
\author Thomas.Hoehenleitner [at] seerose.net
3+
*******************************************************************************/
4+
5+
#ifndef TRICE_UART_H_
6+
#define TRICE_UART_H_
7+
8+
#ifdef __cplusplus
9+
extern "C" {
10+
#endif
11+
12+
#include "trice.h"
13+
14+
#if TRICE_DEFERRED_UARTA == 1
15+
16+
//! Check if a new byte can be written into trice transmit register.
17+
//! \retval 0 == not empty
18+
//! \retval !0 == empty
19+
//! User must provide this function.
20+
TRICE_INLINE uint32_t triceTxDataRegisterEmptyUartA(void) {
21+
return 1; // LL_USART_IsActiveFlag_TXE(TRICE_UARTA);
22+
}
23+
24+
//! Write value v into trice transmit register.
25+
//! \param v byte to transmit
26+
//! User must provide this function.
27+
TRICE_INLINE void triceTransmitData8UartA(uint8_t v) {
28+
// LL_USART_TransmitData8(TRICE_UARTA, v);
29+
}
30+
31+
//! Allow interrupt for empty trice data transmit register.
32+
//! User must provide this function.
33+
TRICE_INLINE void triceEnableTxEmptyInterruptUartA(void) {
34+
// LL_USART_EnableIT_TXE(TRICE_UARTA);
35+
}
36+
37+
//! Disallow interrupt for empty trice data transmit register.
38+
//! User must provide this function.
39+
TRICE_INLINE void triceDisableTxEmptyInterruptUartA(void) {
40+
// LL_USART_DisableIT_TXE(TRICE_UARTA);
41+
}
42+
#endif // #if TRICE_DEFERRED_UARTA == 1
43+
44+
#if TRICE_DEFERRED_UARTB == 1
45+
46+
#endif // #if TRICE_DEFERRED_UARTB == 1
47+
48+
#ifdef __cplusplus
49+
}
50+
#endif
51+
52+
#endif /* TRICE_UART_H_ */

0 commit comments

Comments
 (0)