9efa9b56f8858ce437dbc11fdf913ff4d01c7ec4
[openafs.git] / src / gtx / cb_test.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /*
11  * cb_test: A test of the gator text circular buffer package.
12  *------------------------------------------------------------------------*/
13
14 #include <afsconfig.h>
15 #include <afs/param.h>
16
17 RCSID
18     ("$Header$");
19
20 #include "gtxtextcb.h"          /*Module interface */
21
22
23 #include "AFS_component_version_number.c"
24
25 main(argc, argv)
26      int argc;
27      char **argv;
28
29 {                               /*main */
30
31     register afs_int32 code;    /*Return code */
32     struct gator_textcb_hdr *newCB;     /*Ptr to new CB hdr */
33     char buf[1024];             /*Text buffer */
34     int do_debugging;           /*Print out debugging info? */
35     int i, j, k;                /*Loop variables */
36     struct gator_textcb_entry *curr_ent;        /*Ptr to text entry */
37     char *curr_char;            /*Current char to print */
38
39     printf("\nTesting the gator text object circular buffer package\n\n");
40     printf("Enter 1 to turn debugging on, 0 otherwise: ");
41     scanf("%d", &do_debugging);
42     printf("Initializing CB package: debugging %s\n",
43            (do_debugging ? "YES" : "NO"));
44     code = gator_textcb_Init(do_debugging);
45     if (code) {
46         printf("Initialization failed\n");
47         exit(-1);
48     }
49     printf("Creating a CB with up to 100 entries of 80 chars each\n");
50     newCB = gator_textcb_Create(100, 80);
51     if (newCB == (struct gator_textcb_hdr *)0) {
52         printf("Can't create new circular buffer.");
53         exit(-1);
54     }
55
56     /*
57      * Start writing stuff to this circ buff.
58      */
59     printf("First write\n");
60     sprintf(buf, "%s", "0123456789");
61     code = gator_textcb_Write(newCB, buf, strlen(buf), 0, 0);
62     if (code)
63         printf("First write failed, code is %d\n", code);
64
65     printf("Second write, highlighted\n");
66     code = gator_textcb_Write(newCB, buf, 10, 1, 0);
67     if (code)
68         printf("Second write failed, code is %d\n", code);
69
70     printf("Third write, bulk\n");
71     sprintf(buf, "%s",
72             "Now is the time for all good men to come to the aid of their country");
73     code = gator_textcb_Write(newCB, buf, strlen(buf), 0, 1);
74     if (code)
75         printf("Third write failed, code is %d\n", code);
76
77     printf("Writing out 3 blank lines\n");
78     code = gator_textcb_BlankLine(newCB, 3);
79     if (code)
80         printf("Blank lines failed with error %d\n", code);
81
82     /*
83      * Print out the CB status.
84      */
85     printf("\nStatus of the circular buffer after the writes:\n");
86     printf("\tmaxEntriesStored: %d\n", newCB->maxEntriesStored);
87     printf("\tmaxCharsPerEntry: %d\n", newCB->maxCharsPerEntry);
88     printf("\tcurrEnt         : %d\n", newCB->currEnt);
89     printf("\tcurrEntIdx      : %d\n", newCB->currEntIdx);
90     printf("\toldestEnt       : %d\n", newCB->oldestEnt);
91     printf("\toldestEntIdx    : %d\n", newCB->oldestEntIdx);
92
93     printf("\nType in any number to continue: ");
94     scanf("%d", &i);
95
96     curr_ent = newCB->entry + newCB->oldestEntIdx;
97     for (j = 0, i = newCB->oldestEntIdx; j < 5; j++) {
98         printf("\nEntry %d, idx %d\n", curr_ent->ID, i);
99         printf("\thighlight     : %d\n", curr_ent->highlight);
100         printf("\tnumInversions : %d\n", curr_ent->numInversions);
101         printf("\tinversions    : ");
102         for (k = 0; k < GATOR_TEXTCB_MAXINVERSIONS; k++)
103             printf("%d ", curr_ent->inversion[k]);
104         printf("\n");
105         printf("\tcharsUsed     : %d\n", curr_ent->charsUsed);
106         printf("\ttextp         : '");
107         curr_char = curr_ent->textp;
108         for (k = 0; k < curr_ent->charsUsed; k++)
109             printf("%c", *curr_char++);
110         printf("'\n");
111         if (i == newCB->maxEntriesStored - 1) {
112             i = 0;
113             curr_ent = newCB->entry;
114         } else {
115             i++;
116             curr_ent++;
117         }
118     }                           /*for loop */
119
120     printf("\nMaking small writes ('a') to force the buffer to circulate\n");
121     printf("Enter any number to continue: ");
122     scanf("%d", &i);
123     sprintf(buf, "%s", "a");
124     for (i = 0; i < 100; i++) {
125         printf("#");
126         code = gator_textcb_Write(newCB, buf, 1, 0, 1 /*skip */ );
127         if (code)
128             printf("Small write %d failed, code is %d\n", i, code);
129     }
130     printf("\n");
131
132     /*
133      * Print out the CB status.
134      */
135     printf("\nStatus of the circular buffer after the writes:\n");
136     printf("\tmaxEntriesStored: %d\n", newCB->maxEntriesStored);
137     printf("\tmaxCharsPerEntry: %d\n", newCB->maxCharsPerEntry);
138     printf("\tcurrEnt         : %d\n", newCB->currEnt);
139     printf("\tcurrEntIdx      : %d\n", newCB->currEntIdx);
140     printf("\toldestEnt       : %d\n", newCB->oldestEnt);
141     printf("\toldestEntIdx    : %d\n", newCB->oldestEntIdx);
142
143     printf("\nType in any number to continue: ");
144     scanf("%d", &i);
145
146     curr_ent = newCB->entry + newCB->oldestEntIdx;
147     for (j = 0, i = newCB->oldestEntIdx; j < 100; j++) {
148         printf("\nEntry %d, idx %d\n", curr_ent->ID, i);
149         printf("\thighlight     : %d\n", curr_ent->highlight);
150         printf("\tnumInversions : %d\n", curr_ent->numInversions);
151         printf("\tinversions    : ");
152         for (k = 0; k < GATOR_TEXTCB_MAXINVERSIONS; k++)
153             printf("%d ", curr_ent->inversion[k]);
154         printf("\n");
155         printf("\tcharsUsed     : %d\n", curr_ent->charsUsed);
156         printf("\ttextp         : '");
157         curr_char = curr_ent->textp;
158         for (k = 0; k < curr_ent->charsUsed; k++)
159             printf("%c", *curr_char++);
160         printf("'\n");
161         if (i == newCB->maxEntriesStored - 1) {
162             i = 0;
163             curr_ent = newCB->entry;
164         } else {
165             i++;
166             curr_ent++;
167         }
168     }                           /*for loop */
169
170     /*
171      * Clean-up time!
172      */
173     code = gator_textcb_Delete(newCB);
174     if (code) {
175         printf("CB deletion failed, code is %d\n", code);
176         exit(-1);
177     }
178
179     /*
180      * It worked, mon!  Goombay!!
181      */
182     exit(0);
183
184 }                               /*main */