qtest.c (4720B)
1 #include "portmidi.h" 2 #include "pmutil.h" 3 #include "stdlib.h" 4 #include "stdio.h" 5 6 7 /* make_msg -- make a psuedo-random message of length n whose content 8 * is purely a function of i 9 */ 10 void make_msg(long msg[], int n, int i) 11 { 12 int j; 13 for (j = 0; j < n; j++) { 14 msg[j] = i % (j + 5); 15 } 16 } 17 18 19 /* print_msg -- print the content of msg of length n to stdout */ 20 /**/ 21 void print_msg(long msg[], int n) 22 { 23 int i; 24 for (i = 0; i < n; i++) { 25 printf(" %li", msg[i]); 26 } 27 } 28 29 30 /* cmp_msg -- compare two messages of length n */ 31 /**/ 32 int cmp_msg(long msg[], long msg2[], int n, int i) 33 { 34 int j; 35 for (j = 0; j < n; j++) { 36 if (msg[j] != msg2[j]) { 37 printf("Received message %d doesn't match sent message\n", i); 38 printf("in: "); print_msg(msg, n); printf("\n"); 39 printf("out:"); print_msg(msg2, n); printf("\n"); 40 return FALSE; 41 } 42 } 43 return TRUE; 44 } 45 46 47 int main() 48 { 49 int msg_len; 50 for (msg_len = 4; msg_len < 100; msg_len += 5) { 51 PmQueue *queue = Pm_QueueCreate(100, msg_len * sizeof(long)); 52 int i; 53 long msg[100]; 54 long msg2[100]; 55 56 printf("msg_len = %d\n", msg_len); 57 if (!queue) { 58 printf("Could not allocate queue\n"); 59 return 1; 60 } 61 62 /* insert/remove 1000 messages */ 63 printf("test 1\n"); 64 for (i = 0; i < 1357; i++) { 65 make_msg(msg, msg_len, i); 66 if (Pm_Enqueue(queue, msg)) { 67 printf("Pm_Enqueue error\n"); 68 return 1; 69 } 70 if (Pm_Dequeue(queue, msg2) != 1) { 71 printf("Pm_Dequeue error\n"); 72 return 1; 73 } 74 if (!cmp_msg(msg, msg2, msg_len, i)) { 75 return 1; 76 } 77 } 78 79 /* make full */ 80 printf("test 2\n"); 81 for (i = 0; i < 100; i++) { 82 make_msg(msg, msg_len, i); 83 if (Pm_Enqueue(queue, msg)) { 84 printf("Pm_Enqueue error\n"); 85 return 1; 86 } 87 } 88 89 /* alternately remove and insert */ 90 for (i = 100; i < 1234; i++) { 91 make_msg(msg, msg_len, i - 100); /* what we expect */ 92 if (Pm_Dequeue(queue, msg2) != 1) { 93 printf("Pm_Dequeue error\n"); 94 return 1; 95 } 96 if (!cmp_msg(msg, msg2, msg_len, i)) { 97 return 1; 98 } 99 make_msg(msg, msg_len, i); 100 if (Pm_Enqueue(queue, msg)) { 101 printf("Pm_Enqueue error\n"); 102 return 1; 103 } 104 } 105 106 /* remove all */ 107 while (!Pm_QueueEmpty(queue)) { 108 make_msg(msg, msg_len, i - 100); /* what we expect */ 109 if (Pm_Dequeue(queue, msg2) != 1) { 110 printf("Pm_Dequeue error\n"); 111 return 1; 112 } 113 if (!cmp_msg(msg, msg2, msg_len, i)) { 114 return 1; 115 } 116 i++; 117 } 118 if (i != 1334) { 119 printf("Message count error\n"); 120 return 1; 121 } 122 123 /* now test overflow */ 124 printf("test 3\n"); 125 for (i = 0; i < 110; i++) { 126 make_msg(msg, msg_len, i); 127 if (Pm_Enqueue(queue, msg) == pmBufferOverflow) { 128 break; /* this is supposed to execute after 100 messages */ 129 } 130 } 131 for (i = 0; i < 100; i++) { 132 make_msg(msg, msg_len, i); 133 if (Pm_Dequeue(queue, msg2) != 1) { 134 printf("Pm_Dequeue error\n"); 135 return 1; 136 } 137 if (!cmp_msg(msg, msg2, msg_len, i)) { 138 return 1; 139 } 140 } 141 /* we should detect overflow after removing 100 messages */ 142 if (Pm_Dequeue(queue, msg2) != pmBufferOverflow) { 143 printf("Pm_Dequeue overflow expected\n"); 144 return 1; 145 } 146 147 /* after overflow is detected (and cleared), sender can 148 * send again 149 */ 150 /* see if function is restored, also test peek */ 151 printf("test 4\n"); 152 for (i = 0; i < 1357; i++) { 153 long *peek; 154 make_msg(msg, msg_len, i); 155 if (Pm_Enqueue(queue, msg)) { 156 printf("Pm_Enqueue error\n"); 157 return 1; 158 } 159 peek = (long *) Pm_QueuePeek(queue); 160 if (!cmp_msg(msg, peek, msg_len, i)) { 161 return 1; 162 } 163 if (Pm_Dequeue(queue, msg2) != 1) { 164 printf("Pm_Dequeue error\n"); 165 return 1; 166 } 167 if (!cmp_msg(msg, msg2, msg_len, i)) { 168 return 1; 169 } 170 } 171 Pm_QueueDestroy(queue); 172 } 173 return 0; 174 }