blob: 331f9769000cfdd6253a067f8f37b019c84f63b6 [file] [log] [blame]
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -04001// : vi ts=4 sw=4 noet :
2/*
3==================================================================================
4 Copyright (c) 2019 Nokia
5 Copyright (c) 2018-2019 AT&T Intellectual Property.
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11 http://www.apache.org/licenses/LICENSE-2.0
12
13 Unless required by applicable law or agreed to in writing, software
14 distributed under the License is distributed on an "AS IS" BASIS,
15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 See the License for the specific language governing permissions and
17 limitations under the License.
18==================================================================================
19*/
20
21/*
22 Mmemonic: ring_test.c
23 Abstract: Test the ring funcitons.
24 Author: E. Scott Daniels
25 Date: 31 July 2017
26*/
27
28#include <unistd.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <strings.h>
32#include <errno.h>
33#include <string.h>
34#include <stdint.h>
35
36#include "../src/common/include/rmr.h"
37#include "../src/common/include/rmr_agnostic.h"
38#include "../src/common/src/ring_static.c"
39
40
41/*
42 Conduct a series of interleaved tests inserting i-factor
43 values before beginning to pull values (i-factor must be
44 size - 2 smaller than the ring.
45 Returns 0 on success, 1 on insert failure and 2 on pull failure.
46*/
47static int ie_test( void* r, int i_factor, long inserts ) {
48 int i;
49 int* dp;
50 int data[29];
51
52 for( i = 0; i < inserts; i++ ) {
53 data[i%29] = i;
54 if( ! uta_ring_insert( r, &data[i%29] ) ) {
55 fprintf( stderr, "[FAIL] interleaved insert failed on ifactor=%d i=%d\n", i_factor, i );
56 return 1;
57 }
58 if( i > i_factor-1 ) {
59 dp = uta_ring_extract( r );
60 if( *dp != data[(i-i_factor)%29] ) {
61 fprintf( stderr, "[FAIL] interleaved exctract failed on ifactor=%d i=%d expected=%d got=%d\n", i_factor, i, data[(i-i_factor)%29], *dp );
62 return 2;
63 }
64 }
65 }
66 //fprintf( stderr, "[OK] interleaved insert/extract test passed for insert factor %d\n", i_factor );
67
68 return 0;
69}
70
71int main( void ) {
72 void* r;
73 int i;
74 int j;
75 int data[20];
76 int* dp;
77 int size = 18;
78
79 r = uta_mk_ring( 0 ); // should return nil
80 if( r != NULL ) {
81 fprintf( stderr, "[FAIL] attempt to make a ring with size 0 returned a pointer\n" );
82 exit( 1 );
83 }
84 r = uta_mk_ring( -1 ); // should also return nil
85 if( r != NULL ) {
86 fprintf( stderr, "[FAIL] attempt to make a ring with size <0 returned a pointer\n" );
87 exit( 1 );
88 }
89
90 r = uta_mk_ring( 18 );
91 if( r == NULL ) {
92 fprintf( stderr, "[FAIL] unable to make ring with 17 entries\n" );
93 exit( 1 );
94 }
95
96 for( i = 0; i < 20; i++ ) { // test to ensure it reports full when head/tail start at 0
97 data[i] = i;
98 if( ! uta_ring_insert( r, &data[i] ) ) {
99 break;
100 }
101 }
102
103 if( i > size ) {
104 fprintf( stderr, "[FAIL] didn not report table full: i=%d\n", i );
105 exit( 1 );
106 }
107
108 fprintf( stderr, "[OK] reported table full at i=%d as expected\n", i );
109
110
111 for( i = 0; i < size + 3; i++ ) { // ensure they all come back in order, and we don't get 'extras'
112 if( (dp = uta_ring_extract( r )) == NULL ) {
113 if( i < size-1 ) {
114 fprintf( stderr, "[FAIL] nil pointer at i=%d\n", i );
115 exit( 1 );
116 } else {
117 break;
118 }
119 }
120
121 if( *dp != i ) {
122 fprintf( stderr, "[FAIL] data at i=% isnt right; expected %d got %d\n", i, i, *dp );
123 }
124 }
125 if( i > size ) {
126 fprintf( stderr, "[FAIL] got too many values on extract: %d\n", i );
127 exit( 1 );
128 }
129 fprintf( stderr, "[OK] extracted values were sane, got: %d\n", i-1 );
130
131 uta_ring_free( NULL ); // ensure this doesn't blow up
132 uta_ring_free( r );
133 for( i = 2; i < 15; i++ ) {
134 r = uta_mk_ring( 16 );
135 if( ie_test( r, i, 101 ) != 0 ) { // modest number of inserts
136 exit( 1 );
137 }
138
139 uta_ring_free( r );
140 }
141 fprintf( stderr, "[OK] all modest insert/exctract tests pass\n" );
142
143 size = 5;
144 for( j = 0; j < 20; j++ ) {
145 for( i = 2; i < size - 2; i++ ) {
146 r = uta_mk_ring( size );
147 if( ie_test( r, i, 66000 ) != 0 ) { // should force the 16bit head/tail indexes to roll over
148 exit( 1 );
149 }
150
151 uta_ring_free( r );
152 }
153 fprintf( stderr, "[OK] all large insert/exctract tests pass ring size=%d\n", size );
154
155 size++;
156 }
157
158 return 0;
159}