Skip to content

migrate from astyle to clang-format, add tests/ #8455

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 15 commits into from
24 changes: 16 additions & 8 deletions tests/device/libraries/BSTest/src/BSArduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,20 @@ class ArduinoIOHelper
char* buffer = temp;
size_t len = vsnprintf(temp, sizeof(temp), format, arg);
va_end(arg);
if (len > sizeof(temp) - 1) {
if (len > sizeof(temp) - 1)
{
buffer = new char[len + 1];
if (!buffer) {
if (!buffer)
{
return 0;
}
va_start(arg, format);
ets_vsnprintf(buffer, len + 1, format, arg);
va_end(arg);
}
len = m_stream.write((const uint8_t*) buffer, len);
if (buffer != temp) {
if (buffer != temp)
{
delete[] buffer;
}
return len;
Expand All @@ -40,16 +43,20 @@ class ArduinoIOHelper
size_t len = 0;
// Can't use Stream::readBytesUntil here because it can't tell the
// difference between timing out and receiving the terminator.
while (len < dest_size - 1) {
while (len < dest_size - 1)
{
int c = m_stream.read();
if (c < 0) {
if (c < 0)
{
delay(1);
continue;
}
if (c == '\r') {
if (c == '\r')
{
continue;
}
if (c == '\n') {
if (c == '\n')
{
dest[len] = 0;
break;
}
Expand All @@ -64,7 +71,8 @@ class ArduinoIOHelper

typedef ArduinoIOHelper IOHelper;

inline void fatal() {
inline void fatal()
{
ESP.restart();
}

Expand Down
122 changes: 74 additions & 48 deletions tests/device/libraries/BSTest/src/BSArgs.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* Splitting string into tokens, taking quotes and escape sequences into account.
* From https://github.com/espressif/esp-idf/blob/master/components/console/split_argv.c
* Copyright 2016-2017 Espressif Systems (Shanghai) PTE LTD
* Licensed under the Apache License 2.0.
*/
/* Splitting string into tokens, taking quotes and escape sequences into account.
From https://github.com/espressif/esp-idf/blob/master/components/console/split_argv.c
Copyright 2016-2017 Espressif Systems (Shanghai) PTE LTD
Licensed under the Apache License 2.0.
*/

#ifndef BS_ARGS_H
#define BS_ARGS_H
Expand All @@ -18,7 +18,8 @@ namespace protocol

#define SS_FLAG_ESCAPE 0x8

typedef enum {
typedef enum
{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<- I meant this

/* parsing the space between arguments */
SS_SPACE = 0x0,
/* parsing an argument which isn't quoted */
Expand All @@ -40,29 +41,29 @@ typedef enum {


/**
* @brief Split command line into arguments in place
*
* - This function finds whitespace-separated arguments in the given input line.
*
* 'abc def 1 20 .3' -> [ 'abc', 'def', '1', '20', '.3' ]
*
* - Argument which include spaces may be surrounded with quotes. In this case
* spaces are preserved and quotes are stripped.
*
* 'abc "123 456" def' -> [ 'abc', '123 456', 'def' ]
*
* - Escape sequences may be used to produce backslash, double quote, and space:
*
* 'a\ b\\c\"' -> [ 'a b\c"' ]
*
* Pointers to at most argv_size - 1 arguments are returned in argv array.
* The pointer after the last one (i.e. argv[argc]) is set to NULL.
*
* @param line pointer to buffer to parse; it is modified in place
* @param argv array where the pointers to arguments are written
* @param argv_size number of elements in argv_array (max. number of arguments will be argv_size - 1)
* @return number of arguments found (argc)
*/
@brief Split command line into arguments in place

- This function finds whitespace-separated arguments in the given input line.

'abc def 1 20 .3' -> [ 'abc', 'def', '1', '20', '.3' ]

- Argument which include spaces may be surrounded with quotes. In this case
spaces are preserved and quotes are stripped.

'abc "123 456" def' -> [ 'abc', '123 456', 'def' ]

- Escape sequences may be used to produce backslash, double quote, and space:

'a\ b\\c\"' -> [ 'a b\c"' ]

Pointers to at most argv_size - 1 arguments are returned in argv array.
The pointer after the last one (i.e. argv[argc]) is set to NULL.

@param line pointer to buffer to parse; it is modified in place
@param argv array where the pointers to arguments are written
@param argv_size number of elements in argv_array (max. number of arguments will be argv_size - 1)
@return number of arguments found (argc)
*/
inline size_t split_args(char *line, char **argv, size_t argv_size)
{
const int QUOTE = '"';
Expand All @@ -72,70 +73,95 @@ inline size_t split_args(char *line, char **argv, size_t argv_size)
size_t argc = 0;
char *next_arg_start = line;
char *out_ptr = line;
for (char *in_ptr = line; argc < argv_size - 1; ++in_ptr) {
int char_in = (unsigned char) *in_ptr;
if (char_in == 0) {
for (char *in_ptr = line; argc < argv_size - 1; ++in_ptr)
{
int char_in = (unsigned char) * in_ptr;
if (char_in == 0)
{
break;
}
int char_out = -1;

switch (state) {
switch (state)
{
case SS_SPACE:
if (char_in == SPACE) {
if (char_in == SPACE)
{
/* skip space */
} else if (char_in == QUOTE) {
}
else if (char_in == QUOTE)
{
next_arg_start = out_ptr;
state = SS_QUOTED_ARG;
} else if (char_in == ESCAPE) {
}
else if (char_in == ESCAPE)
{
next_arg_start = out_ptr;
state = SS_ARG_ESCAPED;
} else {
}
else
{
next_arg_start = out_ptr;
state = SS_ARG;
char_out = char_in;
}
break;

case SS_QUOTED_ARG:
if (char_in == QUOTE) {
if (char_in == QUOTE)
{
END_ARG();
} else if (char_in == ESCAPE) {
}
else if (char_in == ESCAPE)
{
state = SS_QUOTED_ARG_ESCAPED;
} else {
}
else
{
char_out = char_in;
}
break;

case SS_ARG_ESCAPED:
case SS_QUOTED_ARG_ESCAPED:
if (char_in == ESCAPE || char_in == QUOTE || char_in == SPACE) {
if (char_in == ESCAPE || char_in == QUOTE || char_in == SPACE)
{
char_out = char_in;
} else {
}
else
{
/* unrecognized escape character, skip */
}
state = (split_state_t) (state & (~SS_FLAG_ESCAPE));
state = (split_state_t)(state & (~SS_FLAG_ESCAPE));
break;

case SS_ARG:
if (char_in == SPACE) {
if (char_in == SPACE)
{
END_ARG();
} else if (char_in == ESCAPE) {
}
else if (char_in == ESCAPE)
{
state = SS_ARG_ESCAPED;
} else {
}
else
{
char_out = char_in;
}
break;
}
/* need to output anything? */
if (char_out >= 0) {
if (char_out >= 0)
{
*out_ptr = char_out;
++out_ptr;
}
}
/* make sure the final argument is terminated */
*out_ptr = 0;
/* finalize the last argument */
if (state != SS_SPACE && argc < argv_size - 1) {
if (state != SS_SPACE && argc < argv_size - 1)
{
argv[argc++] = next_arg_start;
}
/* add a NULL at the end of argv */
Expand Down
33 changes: 21 additions & 12 deletions tests/device/libraries/BSTest/src/BSProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void output_check_failure(IO& io, size_t line)
}

template<typename IO>
void output_test_end(IO& io, bool success, size_t checks, size_t failed_checks, size_t line=0)
void output_test_end(IO& io, bool success, size_t checks, size_t failed_checks, size_t line = 0)
{
io.printf(BS_LINE_PREFIX "end line=%d result=%d checks=%d failed_checks=%d\n", line, success, checks, failed_checks);
}
Expand Down Expand Up @@ -63,40 +63,48 @@ void output_getenv_result(IO& io, const char* key, const char* value)
template<typename IO>
void output_pretest_result(IO& io, bool res)
{
io.printf(BS_LINE_PREFIX "pretest result=%d\n", res?1:0);
io.printf(BS_LINE_PREFIX "pretest result=%d\n", res ? 1 : 0);
}

template<typename IO>
bool input_handle(IO& io, char* line_buf, size_t line_buf_size, int& test_num)
{
int cb_read = io.read_line(line_buf, line_buf_size);
if (cb_read == 0 || line_buf[0] == '\n') {
if (cb_read == 0 || line_buf[0] == '\n')
{
return false;
}
char* argv[4];
size_t argc = split_args(line_buf, argv, sizeof(argv)/sizeof(argv[0]));
if (argc == 0) {
size_t argc = split_args(line_buf, argv, sizeof(argv) / sizeof(argv[0]));
if (argc == 0)
{
return false;
}
if (strcmp(argv[0], "setenv") == 0) {
if (argc != 3) {
if (strcmp(argv[0], "setenv") == 0)
{
if (argc != 3)
{
return false;
}
setenv(argv[1], argv[2], 1);
output_setenv_result(io, argv[1], argv[2]);
test_num = -1;
return false; /* we didn't get the test number yet, so return false */
}
if (strcmp(argv[0], "getenv") == 0) {
if (argc != 2) {
if (strcmp(argv[0], "getenv") == 0)
{
if (argc != 2)
{
return false;
}
const char* value = getenv(argv[1]);
output_getenv_result(io, argv[1], (value != NULL) ? value : "");
return false;
}
if (strcmp(argv[0], "pretest") == 0) {
if (argc != 1) {
if (strcmp(argv[0], "pretest") == 0)
{
if (argc != 1)
{
return false;
}
bool res = ::pretest();
Expand All @@ -106,7 +114,8 @@ bool input_handle(IO& io, char* line_buf, size_t line_buf_size, int& test_num)
/* not one of the commands, try to parse as test number */
char* endptr;
test_num = (int) strtol(argv[0], &endptr, 10);
if (endptr != argv[0] + strlen(argv[0])) {
if (endptr != argv[0] + strlen(argv[0]))
{
return false;
}
return true;
Expand Down
9 changes: 6 additions & 3 deletions tests/device/libraries/BSTest/src/BSStdio.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ class StdIOHelper
size_t read_line(char* dest, size_t dest_size)
{
char* res = fgets(dest, dest_size, stdin);
if (res == NULL) {
if (res == NULL)
{
return 0;
}
size_t len = strlen(dest);
if (dest[len - 1] == '\n') {
if (dest[len - 1] == '\n')
{
dest[len - 1] = 0;
len--;
}
Expand All @@ -39,7 +41,8 @@ class StdIOHelper

typedef StdIOHelper IOHelper;

inline void fatal() {
inline void fatal()
{
throw std::runtime_error("fatal error");
}

Expand Down
Loading