Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/g2cio.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ int
g2c_file_io(FILE *f, int write, int g2ctype, void *var)
{
void *void_be;
char *bvar = NULL;
signed char *bvar = NULL;
short *svar = NULL;
int *ivar = NULL;
long long int *i64var = NULL;
Expand Down
11 changes: 5 additions & 6 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,11 @@ endif()
# Build a C program test.
function(g2c_build_test name)
add_executable(${name} ${name}.c g2c_test_util.c)
target_link_libraries(${name} PUBLIC g2c::g2c)
endfunction()

# Build a C program test.
function(g2c_build_test name)
add_executable(${name} ${name}.c g2c_test_util.c)
if("${name}" STREQUAL "tst_io")
if(CMAKE_C_COMPILER_ID MATCHES "^(GNU|Clang|AppleClang)$")
set_source_files_properties(${name}.c PROPERTIES COMPILE_FLAGS "-Wno-error=pointer-sign")
endif()
endif()
target_link_libraries(${name} PUBLIC g2c::g2c)
endfunction()

Expand Down
33 changes: 29 additions & 4 deletions tests/tst_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,22 +137,31 @@ main()
{
FILE *f;
unsigned char val = 250;
char neg_val = -120;
char val_in;
signed char neg_val = -120;
signed char val_in;
unsigned char uval_in;
int ret;

/* Open the test file. */
if (!(f = fopen(TEST_FILE, "wb")))
return G2C_EFILE;

/* Write 1-byte ints, thrice. */
/* Write 1-byte ints. */
if ((ret = g2c_file_io_ubyte(f, G2C_FILE_WRITE, &val)))
return ret;
if ((ret = g2c_file_io_byte(f, G2C_FILE_WRITE, &neg_val)))
return ret;
if ((ret = g2c_file_io_ubyte(f, G2C_FILE_WRITE, &val)))
return ret;
/* This fourth write mimics writing a signed char as unsigned
* which is the same as defining a generic char on ARM-based
* Linux. The variable neg_val has a value of -120, which
* has a bit representation of "10001000" using two's
* complement. Writing this in accordance with the GRIB2
* standard will perform some bit shifting/manipulation.
*/
if ((ret = g2c_file_io_ubyte(f, G2C_FILE_WRITE, &neg_val)))
return ret;

/* Close file. */
fclose(f);
Expand All @@ -161,7 +170,7 @@ main()
if (!(f = fopen(TEST_FILE, "rb")))
return G2C_EFILE;

/* Read three values. */
/* Read four values. */
if ((ret = g2c_file_io_ubyte(f, G2C_FILE_READ, &uval_in)))
return ret;
if (uval_in != val)
Expand All @@ -174,6 +183,22 @@ main()
return ret;
if (uval_in != val)
return G2C_ERROR;
/* Now lets read the fourth value. Recall it was a signed
* char (-120) written using g2c_file_io_ubyte().
*
* Now we will read using the unsigned byte function because
* we want to mimic read as a generic char, which is unsigned
* on ARM-based Linux. An unsigned char with a bit representation
* of "10001000" which is a value of 136.
*/
if ((ret = g2c_file_io_ubyte(f, G2C_FILE_READ, &uval_in)))
return ret;
/* The test...value read in must not be equal to the original
* negative value; and must be equal to 136; and when cast to
* an unsigned char must equal the original negative value.
*/
if (uval_in != neg_val && uval_in == 136 && (signed char)uval_in == neg_val)
return G2C_NOERROR;

/* Close file. */
fclose(f);
Expand Down
Loading