blob: e0c9b6e6ca0dfd75bd267fb77c9771a311254d82 [file] [log] [blame]
E. Scott Daniels8dd46412019-04-16 20:47:54 +00001// : vi ts=4 sw=4 noet :
2/*
3==================================================================================
E. Scott Daniels8790bf02019-04-23 12:59:28 +00004 Copyright (c) 2019 Nokia
5 Copyright (c) 2018-2019 AT&T Intellectual Property.
E. Scott Daniels8dd46412019-04-16 20:47:54 +00006
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
E. Scott Daniels8790bf02019-04-23 12:59:28 +000011 http://www.apache.org/licenses/LICENSE-2.0
E. Scott Daniels8dd46412019-04-16 20:47:54 +000012
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_static_test.c
23 Abstract: Test the ring funcitons. These are meant to be included at compile
E. Scott Daniels8790bf02019-04-23 12:59:28 +000024 time by the test driver.
E. Scott Daniels8dd46412019-04-16 20:47:54 +000025
26 Author: E. Scott Daniels
27 Date: 3 April 2019
28*/
29
30#include <unistd.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <strings.h>
34#include <errno.h>
35#include <string.h>
36#include <stdint.h>
37
38#include "../src/common/include/rmr.h"
39#include "../src/common/include/rmr_agnostic.h"
40//#include "../src/common/src/ring_static.c"
41
42
43/*
E. Scott Daniels8790bf02019-04-23 12:59:28 +000044 Conduct a series of interleaved tests inserting i-factor
E. Scott Daniels8dd46412019-04-16 20:47:54 +000045 values before beginning to pull values (i-factor must be
E. Scott Daniels8790bf02019-04-23 12:59:28 +000046 size - 2 smaller than the ring.
E. Scott Daniels8dd46412019-04-16 20:47:54 +000047 Returns 0 on success, 1 on insert failure and 2 on pull failure.
48*/
49static int ie_test( void* r, int i_factor, long inserts ) {
50 int i;
51 int* dp;
52 int data[29];
53
54 for( i = 0; i < inserts; i++ ) {
55 data[i%29] = i;
56 if( ! uta_ring_insert( r, &data[i%29] ) ) {
57 fprintf( stderr, "<FAIL> interleaved insert failed on ifactor=%d i=%d\n", i_factor, i );
58 return 1;
59 }
60 if( i > i_factor-1 ) {
61 dp = uta_ring_extract( r );
62 if( *dp != data[(i-i_factor)%29] ) {
63 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 );
64 return 2;
65 }
66 }
67 }
68 //fprintf( stderr, "<OK> interleaved insert/extract test passed for insert factor %d\n", i_factor );
69
70 return 0;
71}
72
73static int ring_test( ) {
74 void* r;
75 int i;
76 int j;
77 int data[20];
78 int* dp;
79 int size = 18;
80
81 r = uta_mk_ring( 0 ); // should return nil
82 if( r != NULL ) {
83 fprintf( stderr, "<FAIL> attempt to make a ring with size 0 returned a pointer\n" );
84 return 1;
85 }
86 r = uta_mk_ring( -1 ); // should also return nil
87 if( r != NULL ) {
88 fprintf( stderr, "<FAIL> attempt to make a ring with size <0 returned a pointer\n" );
89 return 1;
90 }
91
92 r = uta_mk_ring( 18 );
93 if( r == NULL ) {
94 fprintf( stderr, "<FAIL> unable to make ring with 17 entries\n" );
95 return 1;
96 }
97
98 for( i = 0; i < 20; i++ ) { // test to ensure it reports full when head/tail start at 0
99 data[i] = i;
100 if( ! uta_ring_insert( r, &data[i] ) ) {
101 break;
102 }
103 }
104
105 if( i > size ) {
106 fprintf( stderr, "<FAIL> didn not report table full: i=%d\n", i );
107 return 1;
108 }
109
110 fprintf( stderr, "<OK> reported table full at i=%d as expected\n", i );
111
112
113 for( i = 0; i < size + 3; i++ ) { // ensure they all come back in order, and we don't get 'extras'
114 if( (dp = uta_ring_extract( r )) == NULL ) {
115 if( i < size-1 ) {
116 fprintf( stderr, "<FAIL> nil pointer at i=%d\n", i );
117 return 1;
118 } else {
119 break;
120 }
E. Scott Daniels8790bf02019-04-23 12:59:28 +0000121 }
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000122
123 if( *dp != i ) {
124 fprintf( stderr, "<FAIL> data at i=% isnt right; expected %d got %d\n", i, i, *dp );
125 }
126 }
127 if( i > size ) {
128 fprintf( stderr, "<FAIL> got too many values on extract: %d\n", i );
129 return 1;
130 }
131 fprintf( stderr, "<OK> extracted values were sane, got: %d\n", i-1 );
E. Scott Daniels8790bf02019-04-23 12:59:28 +0000132
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000133 uta_ring_free( NULL ); // ensure this doesn't blow up
134 uta_ring_free( r );
135 for( i = 2; i < 15; i++ ) {
136 r = uta_mk_ring( 16 );
137 if( ie_test( r, i, 101 ) != 0 ) { // modest number of inserts
138 fprintf( stderr, "<FAIL> ie test for 101 inserts didn't return 0\n" );
139 return 1;
140 }
141
142 uta_ring_free( r );
143 }
144 fprintf( stderr, "<OK> all modest insert/exctract tests pass\n" );
145
146 size = 5;
147 for( j = 0; j < 20; j++ ) {
148 for( i = 2; i < size - 2; i++ ) {
149 r = uta_mk_ring( size );
150 if( ie_test( r, i, 66000 ) != 0 ) { // should force the 16bit head/tail indexes to roll over
151 fprintf( stderr, "<FAIL> ie test for 66K inserts didn't return 0\n" );
152 return 1;
153 }
E. Scott Daniels8790bf02019-04-23 12:59:28 +0000154
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000155 uta_ring_free( r );
156 }
157 fprintf( stderr, "<OK> all large insert/exctract tests pass ring size=%d\n", size );
158
159 size++;
160 }
161
162 fprintf( stderr, "<INFO> all ring tests pass\n" );
163 return 0;
164}