blob: 5bf6795ace0f42a7aaa09f0f40824611e53cd4ba [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>
E. Scott Daniels412d53d2019-05-20 20:00:52 +000037#include <pthread.h>
38#include <semaphore.h>
E. Scott Daniels8dd46412019-04-16 20:47:54 +000039
E. Scott Daniels8dd46412019-04-16 20:47:54 +000040/*
E. Scott Daniels8790bf02019-04-23 12:59:28 +000041 Conduct a series of interleaved tests inserting i-factor
E. Scott Daniels8dd46412019-04-16 20:47:54 +000042 values before beginning to pull values (i-factor must be
E. Scott Daniels8790bf02019-04-23 12:59:28 +000043 size - 2 smaller than the ring.
E. Scott Daniels8dd46412019-04-16 20:47:54 +000044 Returns 0 on success, 1 on insert failure and 2 on pull failure.
45*/
46static int ie_test( void* r, int i_factor, long inserts ) {
47 int i;
48 int* dp;
49 int data[29];
50
51 for( i = 0; i < inserts; i++ ) {
52 data[i%29] = i;
53 if( ! uta_ring_insert( r, &data[i%29] ) ) {
54 fprintf( stderr, "<FAIL> interleaved insert failed on ifactor=%d i=%d\n", i_factor, i );
55 return 1;
56 }
57 if( i > i_factor-1 ) {
58 dp = uta_ring_extract( r );
59 if( *dp != data[(i-i_factor)%29] ) {
60 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 );
61 return 2;
62 }
63 }
64 }
65 //fprintf( stderr, "<OK> interleaved insert/extract test passed for insert factor %d\n", i_factor );
66
67 return 0;
68}
69
70static int ring_test( ) {
71 void* r;
72 int i;
73 int j;
74 int data[20];
75 int* dp;
76 int size = 18;
E. Scott Danielsc1f84f82020-01-22 12:31:07 -050077 int pfd = -1; // pollable file descriptor for the ring
78 int errors = 0;
E. Scott Daniels8dd46412019-04-16 20:47:54 +000079
80 r = uta_mk_ring( 0 ); // should return nil
E. Scott Daniels77526eb2020-09-17 16:39:31 -040081 errors += fail_not_nil( r, "attempt to make a ring with size 0 returned a pointer" );
82
E. Scott Daniels8dd46412019-04-16 20:47:54 +000083 r = uta_mk_ring( -1 ); // should also return nil
E. Scott Daniels77526eb2020-09-17 16:39:31 -040084 errors += fail_not_nil( r, "attempt to make a ring with negative size returned a pointer" );
E. Scott Daniels8dd46412019-04-16 20:47:54 +000085
86 r = uta_mk_ring( 18 );
E. Scott Daniels77526eb2020-09-17 16:39:31 -040087 errors += fail_if_nil( r, "attempt to make a ring with valid size returned a nil pointer" );
E. Scott Daniels8dd46412019-04-16 20:47:54 +000088
E. Scott Danielsc1f84f82020-01-22 12:31:07 -050089 pfd = uta_ring_getpfd( r ); // get pollable file descriptor
E. Scott Daniels77526eb2020-09-17 16:39:31 -040090 errors += fail_if_true( pfd < 0, "pollable file descriptor returned was bad" );
E. Scott Danielsc1f84f82020-01-22 12:31:07 -050091
92 pfd = uta_ring_config( r, 0x03 ); // turn on locking for reads and writes
E. Scott Daniels77526eb2020-09-17 16:39:31 -040093 errors += fail_if_true( pfd != 1, "attempt to enable locking failed" );
E. Scott Danielsc1f84f82020-01-22 12:31:07 -050094
E. Scott Daniels8dd46412019-04-16 20:47:54 +000095 for( i = 0; i < 20; i++ ) { // test to ensure it reports full when head/tail start at 0
96 data[i] = i;
97 if( ! uta_ring_insert( r, &data[i] ) ) {
98 break;
99 }
100 }
101
E. Scott Daniels77526eb2020-09-17 16:39:31 -0400102 errors += fail_if_true( i > size, "ring insert did not report full table" );
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000103
104 for( i = 0; i < size + 3; i++ ) { // ensure they all come back in order, and we don't get 'extras'
105 if( (dp = uta_ring_extract( r )) == NULL ) {
E. Scott Daniels77526eb2020-09-17 16:39:31 -0400106 errors += fail_if_true( i < size-1, "nil pointer on extract from full table" );
107 break;
E. Scott Daniels8790bf02019-04-23 12:59:28 +0000108 }
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000109
E. Scott Daniels77526eb2020-09-17 16:39:31 -0400110 if( fail_if_true( *dp != i, "extracted data is incorrect; see details below" )) {
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000111 fprintf( stderr, "<FAIL> data at i=% isnt right; expected %d got %d\n", i, i, *dp );
E. Scott Daniels77526eb2020-09-17 16:39:31 -0400112 errors++;
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000113 }
114 }
E. Scott Daniels77526eb2020-09-17 16:39:31 -0400115 fail_if_true( i > size, "got too many values from extract loop" );
E. Scott Daniels8790bf02019-04-23 12:59:28 +0000116
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000117 uta_ring_free( NULL ); // ensure this doesn't blow up
118 uta_ring_free( r );
119 for( i = 2; i < 15; i++ ) {
120 r = uta_mk_ring( 16 );
E. Scott Daniels77526eb2020-09-17 16:39:31 -0400121 errors += fail_not_equal( ie_test( r, i, 101 ), 0, "ie test for 101 inserts didn't return 0" );
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000122
123 uta_ring_free( r );
124 }
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000125
126 size = 5;
127 for( j = 0; j < 20; j++ ) {
128 for( i = 2; i < size - 2; i++ ) {
129 r = uta_mk_ring( size );
E. Scott Daniels77526eb2020-09-17 16:39:31 -0400130 errors += fail_not_equal( ie_test( r, i, 66000 ), 0, "ie test for 66K inserts didn't return 0" );
E. Scott Daniels8790bf02019-04-23 12:59:28 +0000131
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000132 uta_ring_free( r );
133 }
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000134
135 size++;
136 }
137
E. Scott Danielsc1f84f82020-01-22 12:31:07 -0500138 return errors;
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000139}