Skip to content

Commit 01a1378

Browse files
Update for g2cio.c and test (#589)
* Update for g2cio.c and test In g2c_file_io(), define chat *bvar as signed char. The generic char on majority of platforms is signed, except ARM Linux where it is unsigned. Updated test for 1-byte ints to add a fourth read/write and test special condition where the value read will not equal what is written that mimics Arm Linux. * Update funciton g2c_file_io_byte() Return last arg to char. * Forget to fix definition in header. * Update for compiling tst_io.c For GCC, adding "-Wno-error=pointer-sign" to allow that behavior. --------- Co-authored-by: Eric Engle <EricEngle-NOAA@users.noreply.github.com> Co-authored-by: Edward Hartnett <38856240+edwardhartnett@users.noreply.github.com>
1 parent b7a5b8b commit 01a1378

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
lines changed

src/g2cio.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ int
4141
g2c_file_io(FILE *f, int write, int g2ctype, void *var)
4242
{
4343
void *void_be;
44-
char *bvar = NULL;
44+
signed char *bvar = NULL;
4545
short *svar = NULL;
4646
int *ivar = NULL;
4747
long long int *i64var = NULL;

tests/CMakeLists.txt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,11 @@ endif()
9696
# Build a C program test.
9797
function(g2c_build_test name)
9898
add_executable(${name} ${name}.c g2c_test_util.c)
99-
target_link_libraries(${name} PUBLIC g2c::g2c)
100-
endfunction()
101-
102-
# Build a C program test.
103-
function(g2c_build_test name)
104-
add_executable(${name} ${name}.c g2c_test_util.c)
99+
if("${name}" STREQUAL "tst_io")
100+
if(CMAKE_C_COMPILER_ID MATCHES "^(GNU|Clang|AppleClang)$")
101+
set_source_files_properties(${name}.c PROPERTIES COMPILE_FLAGS "-Wno-error=pointer-sign")
102+
endif()
103+
endif()
105104
target_link_libraries(${name} PUBLIC g2c::g2c)
106105
endfunction()
107106

tests/tst_io.c

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,22 +137,31 @@ main()
137137
{
138138
FILE *f;
139139
unsigned char val = 250;
140-
char neg_val = -120;
141-
char val_in;
140+
signed char neg_val = -120;
141+
signed char val_in;
142142
unsigned char uval_in;
143143
int ret;
144144

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

149-
/* Write 1-byte ints, thrice. */
149+
/* Write 1-byte ints. */
150150
if ((ret = g2c_file_io_ubyte(f, G2C_FILE_WRITE, &val)))
151151
return ret;
152152
if ((ret = g2c_file_io_byte(f, G2C_FILE_WRITE, &neg_val)))
153153
return ret;
154154
if ((ret = g2c_file_io_ubyte(f, G2C_FILE_WRITE, &val)))
155155
return ret;
156+
/* This fourth write mimics writing a signed char as unsigned
157+
* which is the same as defining a generic char on ARM-based
158+
* Linux. The variable neg_val has a value of -120, which
159+
* has a bit representation of "10001000" using two's
160+
* complement. Writing this in accordance with the GRIB2
161+
* standard will perform some bit shifting/manipulation.
162+
*/
163+
if ((ret = g2c_file_io_ubyte(f, G2C_FILE_WRITE, &neg_val)))
164+
return ret;
156165

157166
/* Close file. */
158167
fclose(f);
@@ -161,7 +170,7 @@ main()
161170
if (!(f = fopen(TEST_FILE, "rb")))
162171
return G2C_EFILE;
163172

164-
/* Read three values. */
173+
/* Read four values. */
165174
if ((ret = g2c_file_io_ubyte(f, G2C_FILE_READ, &uval_in)))
166175
return ret;
167176
if (uval_in != val)
@@ -174,6 +183,22 @@ main()
174183
return ret;
175184
if (uval_in != val)
176185
return G2C_ERROR;
186+
/* Now lets read the fourth value. Recall it was a signed
187+
* char (-120) written using g2c_file_io_ubyte().
188+
*
189+
* Now we will read using the unsigned byte function because
190+
* we want to mimic read as a generic char, which is unsigned
191+
* on ARM-based Linux. An unsigned char with a bit representation
192+
* of "10001000" which is a value of 136.
193+
*/
194+
if ((ret = g2c_file_io_ubyte(f, G2C_FILE_READ, &uval_in)))
195+
return ret;
196+
/* The test...value read in must not be equal to the original
197+
* negative value; and must be equal to 136; and when cast to
198+
* an unsigned char must equal the original negative value.
199+
*/
200+
if (uval_in != neg_val && uval_in == 136 && (signed char)uval_in == neg_val)
201+
return G2C_NOERROR;
177202

178203
/* Close file. */
179204
fclose(f);

0 commit comments

Comments
 (0)