unittests: fix array length error on OSX#6333
Conversation
| static void test_base64_08_encode_16_bytes(void) | ||
| { | ||
| const int buffer_size = 16; | ||
| int buffer_size = 16; |
There was a problem hiding this comment.
Hm, VLAs should not be used if you can help it. I would rather use enum { buffer_size = 16 };.
There was a problem hiding this comment.
yeah, read about that solution, too. But found this a quicker fix, by removing const. Anyway, there are more of these to fix, so I can adapt to the enum - on the other hand its a test not shipped core code of RIOT
| buffer[i] = 'a'; | ||
| } | ||
|
|
||
| const size_t expected_out_size = 24; |
There was a problem hiding this comment.
This one, too. Probably the compiler wouldn't use VLAs in here anyway, because it should be able to deduce that the length is constant, but if it does use VLAs then that it would be a needless waste of stack and code size.
2d58203 to
0796dc3
Compare
441da2d to
3f122cc
Compare
3f122cc to
b6aace4
Compare
|
@Kijewski are you okay with these changes, please note that this PR only intends to fix compile errors revealed on macOS using clang. This is not about enhancing this (or other) tests in general, there might be other things to improve here, but that I leave for another PR |
| { | ||
| const int buffer_size = 16; | ||
| enum { buffer_size = 16 }; | ||
| unsigned char buffer[buffer_size]; |
There was a problem hiding this comment.
Why these are initialized as they are is a little bit strange (one could just use a macro for that), but let's fix this in a separate PR.
There was a problem hiding this comment.
The Idea was to directly see the set size of the the buffer, instead of the need scrolling there and forth to check it.
It helped me a lot during the initial developing.
| static void test_base64_08_encode_16_bytes(void) | ||
| { | ||
| const int buffer_size = 16; | ||
| enum { buffer_size = 16 }; |
There was a problem hiding this comment.
hmmm, don't know if I like this approach.
I won't object, but at least for me its uncommon to initialize arrays with an enum value.
There was a problem hiding this comment.
@smlng, can you elaborate? can you leave a comment about this unusual initialization?
There was a problem hiding this comment.
was suggested by @Kijewski and after some lookup I also found in several blogs online that this is (besides using magic numbers or #define) the way to go.
|
hey all, this PR is about fixing compiler errors to make the tests macOS compatible. This is not about coding style or structure! Regarding the usage of But still, which way ever the point is to make it compile and work on macOS by calming clang ... |
Well the weird coding style of these tests caused the problem with OSX compatibility in the first place IMHO, so fixing the style could also help fixing the problem. The |
Erm... you made constants to variables not literals. What I suggested was something in the likes of static void test_base64_08_encode_16_bytes(void)
{
unsigned char buffer[16];
for (int i = 0; i < 16; ++i) {
buffer[i] = 'a';
}
size_t element_base64_out_size = 24;
unsigned char element_base64_out[24];
size_t required_out_size = 0;
int ret = base64_encode(buffer, buffer_size, \
element_base64_out, &required_out_size);
TEST_ASSERT_EQUAL_INT(BASE64_ERROR_BUFFER_OUT_SIZE, ret);
TEST_ASSERT_EQUAL_INT(16, required_out_size);
ret = base64_encode(buffer, buffer_size, \
element_base64_out, &element_base64_out_size);
TEST_ASSERT_EQUAL_INT(BASE64_SUCCESS, ret);
TEST_ASSERT_EQUAL_INT(expected_out_size, element_base64_out_size);
}
No BCP AFAIK. But enforcing defines for every test value seems rather non-sensical to me. One of the strongest proponents against literals is @OlegHahm as far as I experienced it, maybe he has some opinion on that? |
|
I'm fine with the enum solution, but would prefer to have it documented since it's not very common. |
In my experience it's a very common ideom, because C does not know C++'s |
Maybe in projects with poor naming conventions... ;-b
Maybe the reason why OSX would fail for former solution. |
|
For the record, I don't object merging as is, but if you could think of a nice comment, I would be even happier. |
|
I've to admit, the current solution is IMO sub optimal, too, because
I would go for |
Fine with me. |
b6aace4 to
07953f3
Compare
|
added comment as suggested |
07953f3 to
48a93cb
Compare
fixes multiple errors in
tests-base64unittest on OSX, like the following one: