File: memory_test.c

package info (click to toggle)
globus-common 18.15-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,684 kB
  • sloc: ansic: 36,506; sh: 4,534; makefile: 354; perl: 151
file content (129 lines) | stat: -rw-r--r-- 3,005 bytes parent folder | download | duplicates (3)
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
/*
 * Copyright 1999-2006 University of Chicago
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @file memory_test.c
 * @brief Test case for globus_memory_t
 */

#include "globus_common.h"
#include "globus_thread_common.h"

static globus_memory_t                   mem;

#define MEM_INIT_SIZE      15
#ifndef TARGET_ARCH_WIN32
#define POPS               100000
#else
#define POPS                 1000
#endif

const char * reference_output = 
    "abc.....abc.....abc.....abc.....abc....." 
    "abc.....abc.....abc.....abc.....abc....." 
    "abc.....abc.....abc.....abc.....abc.....";
typedef struct mem_test_s
{
    char a;
    char b;
    char c;
    char x[4];
    
} mem_test_t;

int mem_init(mem_test_t * m, int cnt);

int dump(globus_byte_t * buf, int size);

int globus_memory_test(void)
{
   int                         rc = GLOBUS_SUCCESS;
   mem_test_t *                mem_ptr[POPS];
   int                         cnt = 0;

   printf("1..1\n");

   /**
    * @test
    * Create a globus_memory_t with globus_memory_init(), pop a whole
    * lot of memory off of it, dump the initial set of memory, and then
    * push the nodes back.
    */
   globus_module_activate(GLOBUS_COMMON_MODULE);

   globus_memory_init(&mem,
		      sizeof(mem_test_t),
		      MEM_INIT_SIZE);

   for(cnt = 0; cnt < POPS; cnt++)
   {
       mem_ptr[cnt] = ( mem_test_t *) globus_memory_pop_node(&mem); 
       mem_init(mem_ptr[cnt], cnt);
   }

   /* nodes are aligned on mod 8 boundaries, add 1 to 7 to make 8 */
   rc = dump((globus_byte_t *) mem_ptr[0], MEM_INIT_SIZE * (sizeof(mem_test_t) + 1));
   printf("%s\n", (rc==0)?"ok":"not ok");
   
   globus_memory_push_node(&mem, (globus_byte_t *)mem_ptr[0]);
   
   for(cnt = 1; cnt < POPS; cnt++)
   {
       globus_memory_push_node(&mem, (globus_byte_t *) mem_ptr[cnt]);
   }

   globus_memory_destroy(&mem);

   globus_module_deactivate(GLOBUS_COMMON_MODULE);

   return rc;
}
int main(int argc, char * argv[])
{
    return globus_memory_test();
}

int
mem_init(mem_test_t * m, int cnt)
{
    memset(m, 0, sizeof(mem_test_t) + 1);
    m->a = 'a';
    m->b = 'b';
    m->c = 'c';
 
    return GLOBUS_TRUE;
}

int dump(globus_byte_t * buf, int size)
{
    int  ctr;
    int  col = 0;
    int failed=0;
    
    for(ctr = 0; ctr < size; ctr++)
    {
        if (reference_output[ctr] != (isprint(buf[ctr]) ? buf[ctr] : '.'))
        {
            failed++;
        }
	col++;
        if(col >= 40)
	{
	    col = 0;
	}
    }
    return failed;
}