1 // : vi ts=4 sw=4 noet :
3 ==================================================================================
4 Copyright (c) 2019 Nokia
5 Copyright (c) 2018-2019 AT&T Intellectual Property.
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
11 http://www.apache.org/licenses/LICENSE-2.0
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 ==================================================================================
22 Mmemonic: ring_static_test.c
23 Abstract: Test the ring funcitons. These are meant to be included at compile
24 time by the test driver.
26 Author: E. Scott Daniels
38 #include <semaphore.h>
41 #include "rmr_agnostic.h"
45 Conduct a series of interleaved tests inserting i-factor
46 values before beginning to pull values (i-factor must be
47 size - 2 smaller than the ring.
48 Returns 0 on success, 1 on insert failure and 2 on pull failure.
50 static int ie_test( void* r, int i_factor, long inserts ) {
55 for( i = 0; i < inserts; i++ ) {
57 if( ! uta_ring_insert( r, &data[i%29] ) ) {
58 fprintf( stderr, "<FAIL> interleaved insert failed on ifactor=%d i=%d\n", i_factor, i );
61 if( i > i_factor-1 ) {
62 dp = uta_ring_extract( r );
63 if( *dp != data[(i-i_factor)%29] ) {
64 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 );
69 //fprintf( stderr, "<OK> interleaved insert/extract test passed for insert factor %d\n", i_factor );
74 static int ring_test( ) {
82 r = uta_mk_ring( 0 ); // should return nil
84 fprintf( stderr, "<FAIL> attempt to make a ring with size 0 returned a pointer\n" );
87 r = uta_mk_ring( -1 ); // should also return nil
89 fprintf( stderr, "<FAIL> attempt to make a ring with size <0 returned a pointer\n" );
93 r = uta_mk_ring( 18 );
95 fprintf( stderr, "<FAIL> unable to make ring with 17 entries\n" );
99 for( i = 0; i < 20; i++ ) { // test to ensure it reports full when head/tail start at 0
101 if( ! uta_ring_insert( r, &data[i] ) ) {
107 fprintf( stderr, "<FAIL> didn not report table full: i=%d\n", i );
111 fprintf( stderr, "<OK> reported table full at i=%d as expected\n", i );
114 for( i = 0; i < size + 3; i++ ) { // ensure they all come back in order, and we don't get 'extras'
115 if( (dp = uta_ring_extract( r )) == NULL ) {
117 fprintf( stderr, "<FAIL> nil pointer at i=%d\n", i );
125 fprintf( stderr, "<FAIL> data at i=% isnt right; expected %d got %d\n", i, i, *dp );
129 fprintf( stderr, "<FAIL> got too many values on extract: %d\n", i );
132 fprintf( stderr, "<OK> extracted values were sane, got: %d\n", i-1 );
134 uta_ring_free( NULL ); // ensure this doesn't blow up
136 for( i = 2; i < 15; i++ ) {
137 r = uta_mk_ring( 16 );
138 if( ie_test( r, i, 101 ) != 0 ) { // modest number of inserts
139 fprintf( stderr, "<FAIL> ie test for 101 inserts didn't return 0\n" );
145 fprintf( stderr, "<OK> all modest insert/exctract tests pass\n" );
148 for( j = 0; j < 20; j++ ) {
149 for( i = 2; i < size - 2; i++ ) {
150 r = uta_mk_ring( size );
151 if( ie_test( r, i, 66000 ) != 0 ) { // should force the 16bit head/tail indexes to roll over
152 fprintf( stderr, "<FAIL> ie test for 66K inserts didn't return 0\n" );
158 fprintf( stderr, "<OK> all large insert/exctract tests pass ring size=%d\n", size );
163 fprintf( stderr, "<INFO> all ring tests pass\n" );