OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
alloc.c
Go to the documentation of this file.
1 
2 #include <allocate2d.h>
3 
4 #include <check.h>
5 #include <stdint.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 
9 START_TEST(allocate2d){
10  size_t w = 5, h = 10;
11  // NB the order of the for loops
12  char **t1 = allocate2d_char(h, w);
13  for (size_t i=0;i<h;i++){
14  for (size_t j=0;j<w;j++){
15  t1[i][j] = (i * w) + j;
16  }
17  }
18  for (size_t i=0;i<h;i++){
19  for (size_t j=0;j<w;j++){
20  ck_assert_int_eq(t1[i][j], (i * w) + j);
21  }
22  }
23  free2d_char(t1);
24 }
25 END_TEST
26 
28  size_t w = 5, h = 10;
29  // NB the order of the for loops
30  char **t1 = (char**)malloc(w * sizeof(char*));
31  for (size_t i=0;i<w;i++){
32  t1[i] = (char*)malloc(h * sizeof(char));
33  }
34  for (size_t i=0;i<w;i++){
35  for (size_t j=0;j<h;j++){
36  t1[i][j] = (i * w) + j;
37  }
38  }
39  for (size_t i=0;i<w;i++){
40  for (size_t j=0;j<h;j++){
41  fprintf(stdout, "%ld, %ld | %d == %ld\n", i, j, t1[i][j], (i * w) + j);
42  ck_assert_int_eq(t1[i][j], (i * w) + j);
43  }
44  }
45  free2d_all_char(t1, w);
46 }
47 END_TEST
48 
49 Suite* stub_suite(void){
50  Suite *s = suite_create("Stub");
51 
52  TCase *tc_core = tcase_create("Core");
53  tcase_add_test(tc_core, allocate2d);
54  tcase_add_test(tc_core, free_all);
55  suite_add_tcase(s, tc_core);
56 
57  return s;
58 }
59 
60 int main(int argc, char **argv){
61  int number_failed;
62 
63  Suite *s = stub_suite();
64  SRunner *sr = srunner_create(s);
65 
66  srunner_run_all(sr, CK_VERBOSE);
67  number_failed = srunner_ntests_failed(sr);
68  srunner_free(sr);
69  return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
70 }
71 
#define EXIT_SUCCESS
Definition: GEO_basic.h:72
int j
Definition: decode_rs.h:73
END_TEST Suite * stub_suite(void)
Definition: alloc.c:49
float h[MODELMAX]
Definition: atrem_corl1.h:131
START_TEST(allocate2d)
Definition: alloc.c:9
int main(int argc, char **argv)
Definition: alloc.c:60
void free2d_char(char **p)
Free a two-dimensional array created by allocate2d_char.
Definition: allocate2d.c:30
Utility functions for allocating and freeing two-dimensional arrays of various types.
void free_all(void *dark_rest, void *gain, void *tdi, void *scan_temp, void *side, void *msec)
data_t s[NROOTS]
Definition: decode_rs.h:75
void free2d_all_char(char **p, size_t length)
Free a two-dimensional array created by malloc'ing each row individually.
Definition: allocate2d.c:34
int i
Definition: decode_rs.h:71
char ** allocate2d_char(size_t h, size_t w)
Allocate a two-dimensional array of type char of a given size.
Definition: allocate2d.c:13