Fix pack compile problems caused by strlen
- Removed strlen usage - Added unit testing to protect the change - Fixed some bugs found with the unit test
This commit is contained in:
parent
88a2e90f8b
commit
08ada5fc7a
3 changed files with 90 additions and 8 deletions
|
@ -149,21 +149,47 @@ void packReadInt(PackStream* stream, int* value)
|
|||
void packWriteString(PackStream* stream, char* value, int max_length)
|
||||
{
|
||||
int written;
|
||||
int len = strnlen(value, max_length - 1) + 1;
|
||||
int len = 0;
|
||||
while (len < max_length - 1 && value[len] != '\0')
|
||||
{
|
||||
len++;
|
||||
}
|
||||
packWriteInt(stream, &len);
|
||||
if (len > 0)
|
||||
{
|
||||
written = fwrite(value, 1, len, stream->fd);
|
||||
assert(written == len);
|
||||
}
|
||||
}
|
||||
|
||||
void packReadString(PackStream* stream, char* value, int max_length)
|
||||
{
|
||||
int read;
|
||||
int len;
|
||||
int len, clen;
|
||||
|
||||
packReadInt(stream, &len);
|
||||
if (len > max_length)
|
||||
|
||||
if (len > max_length - 1)
|
||||
{
|
||||
len = max_length;
|
||||
clen = max_length - 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
clen = len;
|
||||
}
|
||||
|
||||
if (clen > 0)
|
||||
{
|
||||
read = fread(value, 1, clen, stream->fd);
|
||||
assert(read == clen);
|
||||
value[clen] = '\0';
|
||||
|
||||
if (clen < len)
|
||||
{
|
||||
/* Read rest of the string, discarding it */
|
||||
char* buffer = malloc(len - clen);
|
||||
fread(buffer, 1, len - clen, stream->fd);
|
||||
free(buffer);
|
||||
}
|
||||
}
|
||||
read = fread(value, 1, len, stream->fd);
|
||||
assert(read == len);
|
||||
}
|
||||
|
|
|
@ -12,6 +12,7 @@ extern void test_render_case(Suite* s);
|
|||
extern void test_noise_case(Suite* s);
|
||||
extern void test_terrain_painting_case(Suite* s);
|
||||
extern void test_bruneton_case(Suite* s);
|
||||
extern void test_pack_case(Suite* s);
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
|
@ -30,6 +31,7 @@ int main(int argc, char** argv)
|
|||
test_noise_case(s);
|
||||
test_terrain_painting_case(s);
|
||||
test_bruneton_case(s);
|
||||
test_pack_case(s);
|
||||
|
||||
SRunner *sr = srunner_create(s);
|
||||
srunner_run_all(sr, CK_NORMAL);
|
||||
|
|
54
src/testing/test_pack.c
Normal file
54
src/testing/test_pack.c
Normal file
|
@ -0,0 +1,54 @@
|
|||
#include "testing/common.h"
|
||||
|
||||
#include "rendering/tools/pack.h"
|
||||
|
||||
START_TEST(testPack)
|
||||
{
|
||||
PackStream* stream;
|
||||
int i;
|
||||
int data_i;
|
||||
double data_d;
|
||||
char* data_s;
|
||||
char buffer[100];
|
||||
|
||||
/* Writing to pack */
|
||||
stream = packWriteFile("/tmp/test_paysages_pack");
|
||||
for (i = 0; i < 5; i++)
|
||||
{
|
||||
data_i = i;
|
||||
packWriteInt(stream, &data_i);
|
||||
|
||||
data_d = (double)i;
|
||||
packWriteDouble(stream, &data_d);
|
||||
|
||||
data_s = "Testing string 0123 !";
|
||||
packWriteString(stream, data_s, 100);
|
||||
packWriteString(stream, data_s, 4);
|
||||
packWriteString(stream, data_s, 5);
|
||||
}
|
||||
packCloseStream(stream);
|
||||
|
||||
/* Reading from pack */
|
||||
stream = packReadFile("/tmp/test_paysages_pack");
|
||||
for (i = 0; i < 5; i++)
|
||||
{
|
||||
packReadInt(stream, &data_i);
|
||||
ck_assert_int_eq(data_i, i);
|
||||
|
||||
packReadDouble(stream, &data_d);
|
||||
ck_assert_double_eq(data_d, (double)i);
|
||||
|
||||
packReadString(stream, buffer, 100);
|
||||
ck_assert_str_eq(buffer, "Testing string 0123 !");
|
||||
packReadString(stream, buffer, 4);
|
||||
ck_assert_str_eq(buffer, "Tes");
|
||||
packReadString(stream, buffer, 3);
|
||||
ck_assert_str_eq(buffer, "Te");
|
||||
}
|
||||
packCloseStream(stream);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
TEST_CASE(pack,
|
||||
testPack)
|
||||
|
Loading…
Reference in a new issue