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