File: test_alloc_big_array.c

package info (click to toggle)
inhomog 0.1.9.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, forky, sid, trixie
  • size: 2,476 kB
  • sloc: ansic: 10,454; sh: 4,380; makefile: 173
file content (174 lines) | stat: -rw-r--r-- 4,720 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
   alloc_big_array - allocate or free big multi-dimensional arrays

   Copyright (C) 2014-2018 Boud Roukema

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software Foundation,
   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

   See also http://www.gnu.org/licenses/gpl.html
*/

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <stdint.h>

#ifndef _MATH_H
#include <math.h>
#endif

/* #include "lib/superclusbao.h" */
#include "lib/alloc_big_array.h"
#include "config.h"

/* e.g. for malloc_usable_size() ... */
#if defined(__GNUC__) && !defined(_MALLOC_H)
#include <malloc.h>
#endif

/* uint64_t: Although these _BIG numbers cannot be too big for routine
   testing (and as dimensions successively go above 1, must be
   successively smaller even on machines with huge memories), they are
   used in allocation routines, which require 64-bit integers for
   sizes. */
#define M_BIG ((uint64_t)10)
#define N_BIG ((uint64_t)10)
#define P_BIG ((uint64_t)100)
#define Q_BIG ((uint64_t)100)
/* #define Q_BIG ((uint64_t)150000) */

int main(void){

  double * big_array_1D;
  double ** big_array_2D;
  double *** big_array_3D;
  double **** big_array_4D;
  unsigned int i=0,j=0,k=0,l=0;
  int initialise_to_zero = 1;
  int allocation_OK;
  int sum=0;
  int pass=0;

  /* 1D */
  allocation_OK= alloc_big_array_1D_d(P_BIG,
                                      &big_array_1D,
                                      initialise_to_zero);
  printf("\nwill allocate big 1D array..., return val = %d\n",
         allocation_OK);
  if(0==allocation_OK){
    for(k=0;k<P_BIG;k++){
      big_array_1D[k] = k+1;
    };
    for(k=0;k<P_BIG;k++){
      sum += (int)big_array_1D[k];
    };
    printf("Gauss' school sum is %d\n", sum);

    printf("will deallocate big 1D array..., return val = %d\n",
           free_big_array_1D_d(big_array_1D));
    if(sum!=5050){
      pass+=1;
    }
  }else{
    pass+=16;
  };

  /* 2D */
  allocation_OK = alloc_big_array_2D_d(N_BIG,P_BIG,
                                       &big_array_2D,
                                       initialise_to_zero);
  printf("will allocate big 2D array..., return val = %d\n",
         allocation_OK);
  if(0==allocation_OK){
    for(k=0;k<P_BIG;k++){
      big_array_2D[j][k] = k+1;
    };
    sum=0;
    for(k=0;k<P_BIG;k++){
      sum += (int)big_array_2D[j][k];
    };
    printf("Gauss' school sum is %d\n", sum);

    printf("will deallocate big 2D array..., return val = %d\n",
           free_big_array_2D_d(N_BIG,
                               big_array_2D));
    if(sum!=5050){
      pass+=2;
    };
  }else{
    pass+=32;
  };



  /* 3D */
  allocation_OK = alloc_big_array_3D_d(M_BIG,N_BIG,P_BIG,
                                       &big_array_3D,
                                       initialise_to_zero);
  printf("will allocate big 3D array..., return val = %d\n",
         allocation_OK);
  if(0==allocation_OK){
    for(k=0;k<P_BIG;k++){
      big_array_3D[i][j][k] = k+1;
    };
    sum=0;
    for(k=0;k<P_BIG;k++){
      sum += (int)big_array_3D[i][j][k];
    };
    printf("Gauss' school sum is %d\n", sum);

    printf("will deallocate big 3D array..., return val = %d\n",
           free_big_array_3D_d(M_BIG,N_BIG,
                               big_array_3D));
    if(sum!=5050){
      pass+=4;
    }
  }else{
    pass+=64;
  };


  /* 4D */
  allocation_OK = alloc_big_array_4D_d(M_BIG,N_BIG,P_BIG,Q_BIG,
                                       &big_array_4D,
                                       initialise_to_zero);
  printf("will allocate big 4D array..., return val = %d\n",
         allocation_OK);
  if(0==allocation_OK){
    k=0;
    for(l=0;l<P_BIG;l++){
      big_array_4D[i][j][k][l] = l+1;
    };
    sum=0;
    for(l=0;l<P_BIG;l++){
      sum += (int)big_array_4D[i][j][k][l];
    };
    printf("Gauss' school sum is %d\n", sum);

    printf("will deallocate big 4D array..., return val = %d\n",
           free_big_array_4D_d(M_BIG,N_BIG,P_BIG,
                               big_array_4D));
    if(sum!=5050){
      pass+=8;
    }
  }else{
    pass+=128;
  };


  printf("pass = %d\n",pass);

  return pass;
}