Skip to content

Commit

Permalink
Fix more stack overflows in test cases on AVR.
Browse files Browse the repository at this point in the history
printf() strings go into ram on AVR, move most
tests to use the common logic in unittests.h that
only prints line numbers on AVR.
  • Loading branch information
PetteriAimonen committed Feb 2, 2020
1 parent 08b8c14 commit 6d278fb
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 28 deletions.
9 changes: 3 additions & 6 deletions tests/backwards_compatibility/decode_legacy.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,14 @@
#include <pb_decode.h>
#include "alltypes_legacy.h"
#include "test_helpers.h"

#define TEST(x) if (!(x)) { \
printf("Test " #x " failed.\n"); \
return false; \
}
#include "unittests.h"

/* This function is called once from main(), it handles
the decoding and checks the fields. */
bool check_alltypes(pb_istream_t *stream, int mode)
{
AllTypes alltypes = {0};
int status = 0;

if (!pb_decode(stream, AllTypes_fields, &alltypes))
return false;
Expand Down Expand Up @@ -169,7 +166,7 @@ bool check_alltypes(pb_istream_t *stream, int mode)

TEST(alltypes.end == 1099);

return true;
return status == 0;
}

int main(int argc, char **argv)
Expand Down
6 changes: 2 additions & 4 deletions tests/common/unittests.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@

/* Elaborate debug messages for normal development */
#define COMMENT(x) printf("\n----" x "----\n");
#define STR(x) #x
#define STR2(x) STR(x)
#define TEST(x) \
if (!(x)) { \
fprintf(stderr, "\033[31;1mFAILED:\033[22;39m " __FILE__ ":" STR2(__LINE__) " " #x "\n"); \
fprintf(stderr, "\033[31;1mFAILED:\033[22;39m %s:%d %s\n", __FILE__, __LINE__, #x); \
status = 1; \
} else { \
printf("\033[32;1mOK:\033[22;39m " #x "\n"); \
printf("\033[32;1mOK:\033[22;39m %s\n", #x); \
}
#endif

9 changes: 3 additions & 6 deletions tests/extensions/decode_extensions.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,14 @@
#include "alltypes.pb.h"
#include "extensions.pb.h"
#include "test_helpers.h"

#define TEST(x) if (!(x)) { \
printf("Test " #x " failed.\n"); \
return 2; \
}
#include "unittests.h"

int main(int argc, char **argv)
{
uint8_t buffer[1024];
size_t count;
pb_istream_t stream;
int status = 0;

AllTypes alltypes = AllTypes_init_zero;
int32_t extensionfield1;
Expand Down Expand Up @@ -55,6 +52,6 @@ int main(int argc, char **argv)
TEST(strcmp(extensionfield2.test1, "test") == 0)
TEST(extensionfield2.test2 == 54321)

return 0;
return status;
}

9 changes: 3 additions & 6 deletions tests/field_size_16_proto3/decode_alltypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,14 @@
#include <pb_decode.h>
#include "alltypes.pb.h"
#include "test_helpers.h"

#define TEST(x) if (!(x)) { \
printf("Test " #x " failed.\n"); \
return false; \
}
#include "unittests.h"

/* This function is called once from main(), it handles
the decoding and checks the fields. */
bool check_alltypes(pb_istream_t *stream, int mode)
{
AllTypes alltypes = AllTypes_init_zero;
int status = 0;

/* Fill with garbage to better detect initialization errors */
memset(&alltypes, 0xAA, sizeof(alltypes));
Expand Down Expand Up @@ -139,7 +136,7 @@ bool check_alltypes(pb_istream_t *stream, int mode)

TEST(alltypes.end == 1099);

return true;
return status == 0;
}

int main(int argc, char **argv)
Expand Down
9 changes: 3 additions & 6 deletions tests/without_64bit/decode_alltypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,15 @@
#include <pb_decode.h>
#include "alltypes.pb.h"
#include "test_helpers.h"

#define TEST(x) if (!(x)) { \
printf("Test " #x " failed.\n"); \
return false; \
}
#include "unittests.h"

/* This function is called once from main(), it handles
the decoding and checks the fields. */
bool check_alltypes(pb_istream_t *stream, int mode)
{
/* Uses _init_default to just make sure that it works. */
AllTypes alltypes = AllTypes_init_default;
int status = 0;

/* Fill with garbage to better detect initialization errors */
memset(&alltypes, 0xAA, sizeof(alltypes));
Expand Down Expand Up @@ -155,7 +152,7 @@ bool check_alltypes(pb_istream_t *stream, int mode)

TEST(alltypes.end == 1099);

return true;
return status == 0;
}

int main(int argc, char **argv)
Expand Down

0 comments on commit 6d278fb

Please sign in to comment.