Skip to content

Commit

Permalink
memory leaks in C tests fixed
Browse files Browse the repository at this point in the history
meson.build blunder fixed
  • Loading branch information
dalekopera committed Oct 30, 2024
1 parent 0814b53 commit 44cd2b2
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 15 deletions.
12 changes: 12 additions & 0 deletions src/makedefs/definitions
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,15 @@ $(PACKAGE) = $(OBJ)/$(package)_main.o
U_TEST = $(OBJ)/u_elfun.o $(OBJ)/u_group.o $(OBJ)/u_range.o
C_TEST = $(OBJ)/c_elfun.o $(OBJ)/c_group.o $(OBJ)/c_range.o
Q_TEST = $(OBJ)/q_elfun.o $(OBJ)/q_group.o $(OBJ)/q_range.o

# valgrind options if needed

#VALGRIND = --tool=memcheck --leak-check=full --show-reachable=yes \
#VALGRIND = -v --tool=memcheck --leak-check=yes --show-reachable=yes \
#--track-origins=yes -s
VALGRIND = -v --tool=memcheck --leak-check=full --show-reachable=yes \
--track-origins=yes -s

# gdb options if needed

GDB = -silent -ex run -ex bt -ex exit
11 changes: 9 additions & 2 deletions src/test/ctest.c
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,7 @@ printf("hello\n");

//here
free(classification);
free(p_name);
free(X_type);
free(H_row);
free(H_col);
Expand All @@ -1212,8 +1213,14 @@ printf("hello\n");
free(result);
free(G_val);
free(G_var);
for (i = 0; i < n; i++)
free(X_names[i]);
free(X_names);
free(X_names_fortran);
for (i = 0; i < m; i++)
free(C_names[i]);
free(C_names);
free(C_names_fortran);
free(equation);
free(linear);
free(INDEX_nz_vector);
Expand Down Expand Up @@ -1319,7 +1326,7 @@ void write_j_dense(ipc_ n, ipc_ m, ipc_ l_j2_1, ipc_ l_j2_2, rpc_ *J2_val) {
printf("\n");

// Print matrix values
for (ipc_ i = 0; i < l_j2_1; i++) {
for (ipc_ i = 0; i < m; i++) {
printf(" * %7d ", i + 1);
for (ipc_ k = 0; k < 4 && j + k < n; k++) {
printf(" %12.4e", J2_val[i+l_j2_1*(j+k)]);
Expand All @@ -1340,7 +1347,7 @@ void write_jt_dense(ipc_ n, ipc_ m, ipc_ l_j2_1, ipc_ l_j2_2, rpc_ *J2_val) {
printf("\n");

// Print matrix values
for (ipc_ i = 0; i < l_j2_1; i++) {
for (ipc_ i = 0; i < n; i++) {
printf(" * %7d ", i + 1);
for (ipc_ k = 0; k < 4 && j + k < m; k++) {
printf(" %12.4e", J2_val[i+l_j2_1*(j+k)]);
Expand Down
17 changes: 13 additions & 4 deletions src/test/ctest_c.c
Original file line number Diff line number Diff line change
Expand Up @@ -1186,6 +1186,7 @@ int main() {
}

free(classification);
free(p_name);
free(X_type);
free(H_row);
free(H_col);
Expand All @@ -1212,8 +1213,14 @@ int main() {
free(result);
free(G_val);
free(G_var);
for (i = 0; i < n; i++)
free(X_names[i]);
free(X_names);
free(X_names_fortran);
for (i = 0; i < m; i++)
free(C_names[i]);
free(C_names);
free(C_names_fortran);
free(equation);
free(linear);
free(INDEX_nz_vector);
Expand Down Expand Up @@ -1316,10 +1323,11 @@ void write_j_dense(ipc_ n, ipc_ m, ipc_ l_j2_1, ipc_ l_j2_2, rpc_ *J2_val) {
printf("\n");

// Print matrix values
for (ipc_ i = 0; i < l_j2_1; i++) {
for (ipc_ i = 0; i < m; i++) {
printf(" * %7d ", i);
for (ipc_ k = 0; k < 4 && j + k < n; k++) {
printf(" %12.4e", J2_val[i+l_j2_1*(j+k)]);
printf(" %12.4e", J2_val[j+k+n*i]);
// printf(" %12.4e", J2_val[i+l_j2_1*(j+k)]);
}
printf("\n");
}
Expand All @@ -1337,10 +1345,11 @@ void write_jt_dense(ipc_ n, ipc_ m, ipc_ l_j2_1, ipc_ l_j2_2, rpc_ *J2_val) {
printf("\n");

// Print matrix values
for (ipc_ i = 0; i < l_j2_1; i++) {
for (ipc_ i = 0; i < n; i++) {
printf(" * %7d ", i);
for (ipc_ k = 0; k < 4 && j + k < m; k++) {
printf(" %12.4e", J2_val[i+l_j2_1*(j+k)]);
printf(" %12.4e", J2_val[j+k+m*i]);
// printf(" %12.4e", J2_val[i+l_j2_1*(j+k)]);
}
printf("\n");
}
Expand Down
101 changes: 97 additions & 4 deletions src/test/makemaster
Original file line number Diff line number Diff line change
Expand Up @@ -193,20 +193,64 @@ run_c_test_cutest_unconstrained: utools test_cutest_unconstrained
cat ../test/c_test_unc.output
rm $(OBJ)/run_c_test

run_c_test_cutest_unconstrained_sanitize: ctools test_cutest_unconstrained
echo " Exhaustive test of unconstrained C tools"
$(CP) ../test/utest.c $(OBJ)/utest.c
cd $(OBJ) ; $(CC) -o utest.o $(CFLAGS) -fsanitize=address -g utest.c
cd $(OBJ) ; $(FORTRAN) $(RUNFFLAGS) $(SPECIAL) -o run_c_test \
$(NOFMAIN) $(C_TEST) utest.o -L$(OBJ) $(LIBS) \
-fsanitize=address -static-libasan
- $(OBJ)/run_c_test
# - $(OBJ)/run_c_test >& ../test/c_test_unc.output
# cat ../test/c_test_unc.output
rm $(OBJ)/run_c_test

run_c_test_cutest_unconstrained_valgrind: ctools test_cutest_unconstrained
echo " Exhaustive test of unconstrained C tools"
$(CP) ../test/utest.c $(OBJ)/utest.c
cd $(OBJ) ; $(CC) -o utest.o $(CFLAGS) -g utest.c
cd $(OBJ) ; $(FORTRAN) $(RUNFFLAGS) $(SPECIAL) -o run_c_test \
$(NOFMAIN) $(C_TEST) utest.o -L$(OBJ) $(LIBS)
- valgrind $(VALGRIND) $(OBJ)/run_c_test
# - valgrind $(VALGRIND) $(OBJ)/run_c_test >& \
# ../test/c_test_unc_valgrind.output
# cat ../test/c_test_unc_valgrind.output
rm $(OBJ)/run_c_test

run_c_test_cutest_constrained: ctools test_cutest_constrained
echo " Exhaustive test of constrained C tools"
$(CP) ../test/ctest.c $(OBJ)/ctest.c
# cd $(OBJ) ; $(CC) -o ctest.o $(CFLAGS) -fsanitize=address -g ctest.c
cd $(OBJ) ; $(CC) -o ctest.o $(CFLAGS) ctest.c
cd $(OBJ) ; $(FORTRAN) $(RUNFFLAGS) $(SPECIAL) -o run_c_test \
$(NOFMAIN) $(C_TEST) ctest.o -L$(OBJ) $(LIBS)
# $(NOFMAIN) $(C_TEST) ctest.o -L$(OBJ) $(LIBS) \
# -fsanitize=address -static-libasan
# - $(OBJ)/run_c_test
- $(OBJ)/run_c_test >& ../test/c_test_con.output
cat ../test/c_test_con.output
rm $(OBJ)/run_c_test

run_c_test_cutest_constrained_sanitize: ctools test_cutest_constrained
echo " Exhaustive test of constrained C tools"
$(CP) ../test/ctest.c $(OBJ)/ctest.c
cd $(OBJ) ; $(CC) -o ctest.o $(CFLAGS) -fsanitize=address -g ctest.c
cd $(OBJ) ; $(FORTRAN) $(RUNFFLAGS) $(SPECIAL) -o run_c_test \
$(NOFMAIN) $(C_TEST) ctest.o -L$(OBJ) $(LIBS) \
-fsanitize=address -static-libasan
- $(OBJ)/run_c_test
# - $(OBJ)/run_c_test >& ../test/c_test_con.output
# cat ../test/c_test_con.output
rm $(OBJ)/run_c_test

run_c_test_cutest_constrained_valgrind: ctools test_cutest_constrained
echo " Exhaustive test of constrained C tools"
$(CP) ../test/ctest.c $(OBJ)/ctest.c
cd $(OBJ) ; $(CC) -o ctest.o $(CFLAGS) -g ctest.c
cd $(OBJ) ; $(FORTRAN) $(RUNFFLAGS) $(SPECIAL) -o run_c_test \
$(NOFMAIN) $(C_TEST) ctest.o -L$(OBJ) $(LIBS)
- valgrind $(VALGRIND) $(OBJ)/run_c_test
# - valgrind $(VALGRIND) $(OBJ)/run_c_test >& \
# ../test/c_test_con_valgrind.output
# cat ../test/c_test_con_valgrind.output
rm $(OBJ)/run_c_test

run_c_test_c_cutest_unconstrained: utools test_cutest_unconstrained
echo " Exhaustive test of 0-based unconstrained C tools"
$(CP) ../test/utest_c.c $(OBJ)/utest_c.c
Expand All @@ -217,17 +261,66 @@ run_c_test_c_cutest_unconstrained: utools test_cutest_unconstrained
cat ../test/c_test_c_unc.output
rm $(OBJ)/run_c_test

run_c_test_c_cutest_unconstrained_sanitize: utools test_cutest_unconstrained
echo " Exhaustive test of 0-based unconstrained C tools"
$(CP) ../test/utest_c.c $(OBJ)/utest_c.c
cd $(OBJ) ; $(CC) -o utest_c.o $(CFLAGS) -fsanitize=address \
-g utest_c.c
cd $(OBJ) ; $(FORTRAN) $(RUNFFLAGS) $(SPECIAL) -o run_c_test \
$(NOFMAIN) $(C_TEST) utest_c.o -L$(OBJ) $(LIBS) \
-fsanitize=address -static-libasan
- $(OBJ)/run_c_test
# - $(OBJ)/run_c_test >& ../test/c_test_c_unc.output
# cat ../test/c_test_c_unc.output
rm $(OBJ)/run_c_test

run_c_test_c_cutest_unconstrained_valgrind: utools test_cutest_unconstrained
echo " Exhaustive test of 0-based unconstrained C tools"
$(CP) ../test/utest_c.c $(OBJ)/utest_c.c
cd $(OBJ) ; $(CC) -o utest_c.o $(CFLAGS) utest_c.c
cd $(OBJ) ; $(FORTRAN) $(RUNFFLAGS) $(SPECIAL) -o run_c_test \
$(NOFMAIN) $(C_TEST) utest_c.o -L$(OBJ) $(LIBS)
- valgrind $(VALGRIND) $(OBJ)/run_c_test
# - valgrind $(VALGRIND) $(OBJ)/run_c_test >& \
# ../test/c_test_c_unc_valgrind.output
# cat ../test/c_test_c_unc_valgrind.output
rm $(OBJ)/run_c_test

run_c_test_c_cutest_constrained: ctools test_cutest_constrained
echo " Exhaustive test of 0-based constrained C tools"
$(CP) ../test/ctest_c.c $(OBJ)/ctest_c.c
cd $(OBJ) ; $(CC) -o ctest_c.o $(CFLAGS) ctest_c.c
cd $(OBJ) ; $(FORTRAN) $(RUNFFLAGS) $(SPECIAL) -o run_c_test \
$(NOFMAIN) $(C_TEST) ctest_c.o -L$(OBJ) $(LIBS)
- $(OBJ)/run_c_test >& ../test/c_test_c_con.output
cat ../test/c_test_c_con.output
rm $(OBJ)/run_c_test

run_c_test_c_cutest_constrained_sanitize: ctools test_cutest_constrained
echo " Exhaustive test of 0-based constrained C tools"
$(CP) ../test/ctest_c.c $(OBJ)/ctest_c.c
cd $(OBJ) ; $(CC) -o ctest_c.o $(CFLAGS) -fsanitize=address \
-g ctest_c.c
cd $(OBJ) ; $(FORTRAN) $(RUNFFLAGS) $(SPECIAL) -o run_c_test \
$(NOFMAIN) $(C_TEST) ctest_c.o -L$(OBJ) $(LIBS) \
-fsanitize=address -static-libasan
- $(OBJ)/run_c_test
# - $(OBJ)/run_c_test >& ../test/c_test_c_con.output
# cat ../test/c_test_c_con.output
rm $(OBJ)/run_c_test

run_c_test_c_cutest_constrained_valgrind: ctools test_cutest_constrained
echo " Exhaustive test of 0-based constrained C tools"
$(CP) ../test/ctest_c.c $(OBJ)/ctest_c.c
cd $(OBJ) ; $(CC) -o ctest_c.o $(CFLAGS) ctest_c.c
cd $(OBJ) ; $(FORTRAN) $(RUNFFLAGS) $(SPECIAL) -o run_c_test \
$(NOFMAIN) $(C_TEST) ctest_c.o -L$(OBJ) $(LIBS)
- valgrind $(VALGRIND) $(OBJ)/run_c_test
# - valgrind $(VALGRIND) $(OBJ)/run_c_test >& \
# ../test/c_test_c_con_valgrind.output
# cat ../test/c_test_c_con_valgrind.output
rm $(OBJ)/run_c_test

tools:
( cd ../tools ; $(MAKE) -f $(CUTEST)/makefiles/$(VERSION) \
cutest PRECIS=$(PRECIS) PWD=$(PWD)/../tools )
Expand Down
6 changes: 3 additions & 3 deletions src/test/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ cutest_tests += [['cutest', 'single', 'ctest_single' , files('ctest.F90'

cutest_c_tests += [['cutest', 'single', 'ctest_c_single' , files('ctest.c' , 'c_elfun_single.f', 'c_group_single.f', 'c_range_single.f')],
['cutest', 'single', 'utest_c_single' , files('utest.c' , 'u_elfun_single.f', 'u_group_single.f', 'u_range_single.f')],
['cutest', 'single', 'ctest2_c_single', files('ctest_c.c', 'u_elfun_single.f', 'u_group_single.f', 'u_range_single.f')],
['cutest', 'single', 'ctest2_c_single', files('ctest_c.c', 'c_elfun_single.f', 'c_group_single.f', 'c_range_single.f')],
['cutest', 'single', 'utest2_c_single', files('utest_c.c', 'u_elfun_single.f', 'u_group_single.f', 'u_range_single.f')],
['cutest', 'double', 'ctest_c_double' , files('ctest.c' , 'c_elfun_double.f', 'c_group_double.f', 'c_range_double.f')],
['cutest', 'double', 'utest_c_double' , files('utest.c' , 'u_elfun_double.f', 'u_group_double.f', 'u_range_double.f')],
['cutest', 'double', 'ctest2_c_double', files('ctest_c.c', 'u_elfun_double.f', 'u_group_double.f', 'u_range_double.f')]]
['cutest', 'double', 'ctest2_c_double', files('ctest_c.c', 'c_elfun_double.f', 'c_group_double.f', 'c_range_double.f')]],
['cutest', 'double', 'utest2_c_double', files('utest_c.c', 'u_elfun_double.f', 'u_group_double.f', 'u_range_double.f')]]

if build_quadruple
Expand All @@ -27,6 +27,6 @@ if build_quadruple

cutest_c_tests += [['cutest', 'quadruple', 'ctest_c_quadruple' , files('ctest.c' , 'c_elfun_quadruple.f', 'c_group_quadruple.f', 'c_range_quadruple.f')],
['cutest', 'quadruple', 'utest_c_quadruple' , files('utest.c' , 'u_elfun_quadruple.f', 'u_group_quadruple.f', 'u_range_quadruple.f')],
['cutest', 'quadruple', 'ctest2_c_quadruple', files('ctest_c.c', 'u_elfun_quadruple.f', 'u_group_quadruple.f', 'u_range_quadruple.f')]]
['cutest', 'quadruple', 'ctest2_c_quadruple', files('ctest_c.c', 'c_elfun_quadruple.f', 'c_group_quadruple.f', 'c_range_quadruple.f')]],
['cutest', 'quadruple', 'utest2_c_quadruple', files('utest_c.c', 'u_elfun_quadruple.f', 'u_group_quadruple.f', 'u_range_quadruple.f')]]
endif
2 changes: 2 additions & 0 deletions src/test/utest.c
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,8 @@ int main() {
// Cleanup and close files
free(classification);
free(p_name);
for (i = 0; i < n; i++)
free(X_names[i]);
free(X_names);
free(X_names_fortran);
free(X);
Expand Down
2 changes: 2 additions & 0 deletions src/test/utest_c.c
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,8 @@ int main() {
// Cleanup and close files
free(classification);
free(p_name);
for (i = 0; i < n; i++)
free(X_names[i]);
free(X_names);
free(X_names_fortran);
free(X);
Expand Down
4 changes: 2 additions & 2 deletions src/tools/cofsg.F90
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
! THIS VERSION: CUTEST 2.3 - 2024-10-23 AT 08:30 GMT.
! THIS VERSION: CUTEST 2.3 - 2024-10-30 AT 08:40 GMT.

#include "cutest_modules.h"
#include "cutest_routines.h"
Expand Down Expand Up @@ -44,7 +44,7 @@ SUBROUTINE CUTEST_cofsg_c_r( status, n, X, f, nnzg, lg, G_val, G_var, &
CALL CUTEST_cofsg_r( status, n, X, f, nnzg, lg, G_val, G_var, &
grad_fortran )

G_var( : nnzg ) = G_var( : nnzg ) - 1
IF ( grad_fortran ) G_var( : nnzg ) = G_var( : nnzg ) - 1

RETURN

Expand Down

0 comments on commit 44cd2b2

Please sign in to comment.