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