-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
1,578 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Attention | ||
|
||
* Do **not** edit `generated_cgoPackage.go`. Change instead file `../testdata/cgoPackage.go` and execute `../updateTestData.sh` afterwards. This influences _all_ cgot packages tests. | ||
* For individual modifications use file `cgo_test.go` or create an additional file. |
429 changes: 429 additions & 0 deletions
429
test/ERROR_dblB_di_nopf_rtt32__de_xtea_cobs_ua/SEGGER_RTT_Conf.h
Large diffs are not rendered by default.
Oops, something went wrong.
33 changes: 33 additions & 0 deletions
33
test/ERROR_dblB_di_nopf_rtt32__de_xtea_cobs_ua/cgo_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package cgot | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"path" | ||
"testing" | ||
|
||
"github.com/rokath/trice/internal/args" | ||
"github.com/spf13/afero" | ||
"github.com/tj/assert" | ||
) | ||
|
||
func TestLogs(t *testing.T) { | ||
|
||
// triceLog is the log function for executing the trice logging on binary log data in buffer as space separated numbers. | ||
// It uses the inside fSys specified til.json and returns the log output. | ||
triceLog0 := func(t *testing.T, fSys *afero.Afero, buffer string) string { | ||
var o bytes.Buffer | ||
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=off", "-pw=MySecret", "-pf=COBS", "-d16"})) | ||
return o.String() | ||
} | ||
|
||
// triceLog is the log function for executing the trice logging on binary log data in buffer as space separated numbers. | ||
// It uses the inside fSys specified til.json and returns the log output. | ||
triceLog1 := func(t *testing.T, fSys *afero.Afero, buffer string) string { | ||
var o bytes.Buffer | ||
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=off", "-pw=MySecret", "-pf=COBS", "-d16=false"})) | ||
return o.String() | ||
} | ||
|
||
triceLogTest2(t, triceLog0, triceLog1, testLines) | ||
} |
238 changes: 238 additions & 0 deletions
238
test/ERROR_dblB_di_nopf_rtt32__de_xtea_cobs_ua/generated_cgoPackage.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,238 @@ | ||
// Copyright 2020 Thomas.Hoehenleitner [at] seerose.net | ||
// Use of this source code is governed by a license that can be found in the LICENSE file. | ||
|
||
// Package cgot is a helper for testing the target C-code. | ||
// Each C function gets a Go wrapper which is tested in appropriate test functions. | ||
// For some reason inside the trice_test.go an 'import "C"' is not possible. | ||
// The C-files referring to the trice sources this way avoiding code duplication. | ||
// The Go functions defined here are not exported. They are called by the Go test functions in this package. | ||
// This way the test functions are executing the trice C-code compiled with the triceConfig.h here. | ||
// Inside ./testdata this file is named cgoPackage.go where it is maintained. | ||
// The test/updateTestData.sh script copied this file under the name generated_cgoPackage.go into various | ||
// package folders, where it is used separately. | ||
package cgot | ||
|
||
// #include <stdint.h> | ||
// void TriceCheck( int n ); | ||
// void TriceTransfer( void ); | ||
// unsigned TriceOutDepth( void ); | ||
// void CgoSetTriceBuffer( uint8_t* buf ); | ||
// void CgoClearTriceBuffer( void ); | ||
// #cgo CFLAGS: -g -I../../src | ||
// #include "../../src/trice.c" | ||
// #include "../../src/trice8.c" | ||
// #include "../../src/trice16.c" | ||
// #include "../../src/trice32.c" | ||
// #include "../../src/trice64.c" | ||
// #include "../../src/triceUart.c" | ||
// #include "../../src/triceAuxiliary.c" | ||
// #include "../../src/triceDoubleBuffer.c" | ||
// #include "../../src/triceRingBuffer.c" | ||
// #include "../../src/triceStackBuffer.c" | ||
// #include "../../src/triceStaticBuffer.c" | ||
// #include "../../src/xtea.c" | ||
// #include "../../src/cobsDecode.c" | ||
// #include "../../src/cobsEncode.c" | ||
// #include "../../src/tcobsv1Decode.c" | ||
// #include "../../src/tcobsv1Encode.c" | ||
// #include "../testdata/triceCheck.c" | ||
// #include "../testdata/cgoTrice.c" | ||
import "C" | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"path" | ||
"runtime" | ||
"strings" | ||
"testing" | ||
"unsafe" | ||
|
||
"github.com/rokath/trice/pkg/msg" | ||
"github.com/spf13/afero" | ||
"github.com/tj/assert" | ||
) | ||
|
||
var ( | ||
triceDir string // triceDir holds the trice directory path. | ||
testLines = -1 // testLines is the common number of tested lines in triceCheck. The value -1 is for all lines, what takes time. | ||
) | ||
|
||
// https://stackoverflow.com/questions/23847003/golang-tests-and-working-directory | ||
func init() { | ||
_, filename, _, _ := runtime.Caller(0) // filename is the test executable inside the package dir like cgo_stackBuffer_noCycle_tcobs | ||
testDir := path.Dir(filename) | ||
triceDir = path.Join(testDir, "../../") | ||
C.TriceInit() | ||
} | ||
|
||
// setTriceBuffer tells the underlying C code where to output the trice byte stream. | ||
func setTriceBuffer(o []byte) { | ||
Cout := (*C.uchar)(unsafe.Pointer(&o[0])) | ||
C.CgoSetTriceBuffer(Cout) | ||
} | ||
|
||
// triceCheck performs triceCheck C-code sequence n. | ||
func triceCheck(n int) { | ||
C.TriceCheck(C.int(n)) | ||
} | ||
|
||
// triceTransfer performs the deferred trice output. | ||
func triceTransfer() { | ||
C.TriceTransfer() | ||
} | ||
|
||
// triceOutDepth returns the actual out buffer depth. | ||
func triceOutDepth() int { | ||
return int(C.TriceOutDepth()) | ||
} | ||
|
||
// triceClearOutBuffer tells the trice kernel, that the data has been red. | ||
func triceClearOutBuffer() { | ||
C.CgoClearTriceBuffer() | ||
} | ||
|
||
// linesInFile does get the lines in a file and stores them in a string slice. | ||
func linesInFile(fh afero.File) []string { // https://www.dotnetperls.com/lines-file-go | ||
// Create new Scanner. | ||
scanner := bufio.NewScanner(fh) | ||
result := []string{} | ||
// Use Scan. | ||
for scanner.Scan() { | ||
line := scanner.Text() | ||
// Append line to result. | ||
result = append(result, line) | ||
} | ||
return result | ||
} | ||
|
||
// results contains the expected result string exps for line number line. | ||
type results struct { | ||
line int | ||
exps string | ||
} | ||
|
||
func getExpectedResults(fSys *afero.Afero, filename string) (result []results) { | ||
// get all file lines into a []string | ||
f, e := fSys.Open(filename) | ||
msg.OnErr(e) | ||
lines := linesInFile(f) | ||
|
||
for i, line := range lines { | ||
s := strings.Split(line, "//") | ||
if len(s) == 2 { // just one "//" | ||
lineEnd := s[1] | ||
subStr := "exp:" | ||
index := strings.LastIndex(lineEnd, subStr) | ||
if index >= 0 { | ||
var r results | ||
r.line = i + 1 // 1st line number is 1 and not 0 | ||
r.exps = strings.TrimSpace(lineEnd[index+len(subStr) : len(lineEnd)]) | ||
result = append(result, r) | ||
} | ||
} | ||
} | ||
return | ||
} | ||
|
||
// logF is the log function type for executing the trice logging on binary log data in buffer as space separated numbers. | ||
// It uses the inside fSys specified til.json and returns the log output. | ||
type logF func(t *testing.T, fSys *afero.Afero, buffer string) string | ||
|
||
// triceLogTest creates a list of expected results from path.Join(triceDir, "./test/testdata/triceCheck.c"). | ||
// It loops over the result list and executes for each result the compiled C-code. | ||
// It passes the received binary data as buffer to the triceLog function of type logF. | ||
// This function is test package specific defined. The file cgoPackage.go is | ||
// copied into all specific test packages and compiled there together with the | ||
// triceConfig.h, which holds the test package specific target code configuration. | ||
// limit is the count of executed test lines starting from the beginning. -1 ist for all. | ||
func triceLogTest(t *testing.T, triceLog logF, limit int) { | ||
|
||
osFSys := &afero.Afero{Fs: afero.NewOsFs()} | ||
//mmFSys := &afero.Afero{Fs: afero.NewMemMapFs()} | ||
|
||
// CopyFileIntoFSys(t, mmFSys, "til.json", osFSys, td+"./til.json") // needed for the trice log | ||
out := make([]byte, 32768) | ||
setTriceBuffer(out) | ||
|
||
result := getExpectedResults(osFSys, path.Join(triceDir, "./test/testdata/triceCheck.c")) | ||
|
||
var count int | ||
for i, r := range result { | ||
|
||
count++ | ||
if limit >= 0 && count >= limit { | ||
return | ||
} | ||
//if r.line == 55 || r.line == 58 { | ||
fmt.Println(i, r) | ||
|
||
// target activity | ||
triceCheck(r.line) | ||
|
||
triceTransfer() // This is only for deferred modes needed, but direct modes contain this as empty function. | ||
|
||
length := triceOutDepth() | ||
bin := out[:length] // bin contains the binary trice data of trice message i in r.line | ||
|
||
buf := fmt.Sprint(bin) | ||
buffer := buf[1 : len(buf)-1] | ||
|
||
act := triceLog(t, osFSys, buffer) | ||
triceClearOutBuffer() | ||
|
||
assert.Equal(t, r.exps, strings.TrimSuffix(act, "\n")) | ||
//} | ||
} | ||
} | ||
|
||
// triceLogTest2 works like triceLogTest but additionally expects doubled output: direct and deferred. | ||
func triceLogTest2(t *testing.T, triceLog0, triceLog1 logF, limit int) { | ||
|
||
osFSys := &afero.Afero{Fs: afero.NewOsFs()} | ||
|
||
// CopyFileIntoFSys(t, mmFSys, "til.json", osFSys, td+"./til.json") // needed for the trice log | ||
out := make([]byte, 32768) | ||
setTriceBuffer(out) | ||
|
||
result := getExpectedResults(osFSys, path.Join(triceDir, "./test/testdata/triceCheck.c")) | ||
|
||
var count int | ||
for i, r := range result { | ||
|
||
count++ | ||
if limit >= 0 && count >= limit { | ||
return | ||
} | ||
fmt.Println(i, r) | ||
triceCheck(r.line) // target activity | ||
|
||
{ // check direct output | ||
length := triceOutDepth() | ||
bin := out[:length] // bin contains the binary trice data of trice message i | ||
|
||
buf := fmt.Sprint(bin) | ||
buffer := buf[1 : len(buf)-1] | ||
|
||
act := triceLog0(t, osFSys, buffer) | ||
triceClearOutBuffer() | ||
|
||
assert.Equal(t, r.exps, strings.TrimSuffix(act, "\n")) | ||
} | ||
|
||
{ // check deferred output | ||
triceTransfer() | ||
|
||
length := triceOutDepth() | ||
bin := out[:length] // bin contains the binary trice data of trice message i | ||
|
||
buf := fmt.Sprint(bin) | ||
buffer := buf[1 : len(buf)-1] | ||
|
||
act := triceLog1(t, osFSys, buffer) | ||
triceClearOutBuffer() | ||
|
||
assert.Equal(t, r.exps, strings.TrimSuffix(act, "\n")) | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
test/ERROR_dblB_di_nopf_rtt32__de_xtea_cobs_ua/triceConfig.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/*! \file triceConfig.h | ||
\author Thomas.Hoehenleitner [at] seerose.net | ||
*******************************************************************************/ | ||
|
||
#ifndef TRICE_CONFIG_H_ | ||
#define TRICE_CONFIG_H_ | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
// trice l -p jlink -args "-Device STM32F030R8 -if SWD -Speed 4000 -RTTChannel 0" -showID "deb:%04x" -hs off -pw MySecret -pf COBS -d16=true | ||
#define TRICE_DIRECT_OUTPUT 1 | ||
#define TRICE_DIRECT_SEGGER_RTT_32BIT_WRITE 1 | ||
|
||
// trice l -p com4 -d16=false -pw MySecret -pf COBS | ||
#define TRICE_DEFERRED_OUTPUT 1 | ||
#define TRICE_DEFERRED_XTEA_ENCRYPT 1 | ||
#define TRICE_DEFERRED_OUT_FRAMING TRICE_FRAMING_COBS | ||
#define TRICE_DEFERRED_UARTA 1 | ||
#define TRICE_UARTA | ||
|
||
#define TRICE_CGO 1 | ||
#define TRICE_CYCLE_COUNTER 0 | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif /* TRICE_CONFIG_H_ */ |
52 changes: 52 additions & 0 deletions
52
test/ERROR_dblB_di_nopf_rtt32__de_xtea_cobs_ua/triceUart.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/*! \file triceUart.h | ||
\author Thomas.Hoehenleitner [at] seerose.net | ||
*******************************************************************************/ | ||
|
||
#ifndef TRICE_UART_H_ | ||
#define TRICE_UART_H_ | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#include "trice.h" | ||
|
||
#if TRICE_DEFERRED_UARTA == 1 | ||
|
||
//! Check if a new byte can be written into trice transmit register. | ||
//! \retval 0 == not empty | ||
//! \retval !0 == empty | ||
//! User must provide this function. | ||
TRICE_INLINE uint32_t triceTxDataRegisterEmptyUartA(void) { | ||
return 1; //LL_USART_IsActiveFlag_TXE(TRICE_UARTA); | ||
} | ||
|
||
//! Write value v into trice transmit register. | ||
//! \param v byte to transmit | ||
//! User must provide this function. | ||
TRICE_INLINE void triceTransmitData8UartA(uint8_t v) { | ||
//LL_USART_TransmitData8(TRICE_UARTA, v); | ||
} | ||
|
||
//! Allow interrupt for empty trice data transmit register. | ||
//! User must provide this function. | ||
TRICE_INLINE void triceEnableTxEmptyInterruptUartA(void) { | ||
//LL_USART_EnableIT_TXE(TRICE_UARTA); | ||
} | ||
|
||
//! Disallow interrupt for empty trice data transmit register. | ||
//! User must provide this function. | ||
TRICE_INLINE void triceDisableTxEmptyInterruptUartA(void) { | ||
//LL_USART_DisableIT_TXE(TRICE_UARTA); | ||
} | ||
#endif // #if TRICE_DEFERRED_UARTA == 1 | ||
|
||
#if TRICE_DEFERRED_UARTB == 1 | ||
|
||
#endif // #if TRICE_DEFERRED_UARTB == 1 | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif /* TRICE_UART_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.