1 #include <assert.h> 2 #include <limits.h> 3 #include <stddef.h> 4 #include <stdio.h> 5 #include <stdlib.h> 6 #include <string.h> 7 8 #include "debug.h" 9 #include "printbuf.h" 10 11 static void test_basic_printbuf_memset(void); 12 static void test_printbuf_memset_length(void); 13 14 #ifndef __func__ 15 /* VC++ compat */ 16 #define __func__ __FUNCTION__ 17 #endif 18 19 static void test_basic_printbuf_memset() 20 { 21 struct printbuf *pb; 22 23 printf("%s: starting test\n", __func__); 24 pb = printbuf_new(); 25 sprintbuf(pb, "blue:%d", 1); 26 printbuf_memset(pb, -1, 'x', 52); 27 printf("Buffer contents:%.*s\n", printbuf_length(pb), pb->buf); 28 printbuf_free(pb); 29 printf("%s: end test\n", __func__); 30 } 31 32 static void test_printbuf_memset_length() 33 { 34 struct printbuf *pb; 35 36 printf("%s: starting test\n", __func__); 37 pb = printbuf_new(); 38 printbuf_memset(pb, -1, ' ', 0); 39 printbuf_memset(pb, -1, ' ', 0); 40 printbuf_memset(pb, -1, ' ', 0); 41 printbuf_memset(pb, -1, ' ', 0); 42 printbuf_memset(pb, -1, ' ', 0); 43 printf("Buffer length: %d\n", printbuf_length(pb)); 44 printbuf_memset(pb, -1, ' ', 2); 45 printbuf_memset(pb, -1, ' ', 4); 46 printbuf_memset(pb, -1, ' ', 6); 47 printf("Buffer length: %d\n", printbuf_length(pb)); 48 printbuf_memset(pb, -1, ' ', 6); 49 printf("Buffer length: %d\n", printbuf_length(pb)); 50 printbuf_memset(pb, -1, ' ', 8); 51 printbuf_memset(pb, -1, ' ', 10); 52 printbuf_memset(pb, -1, ' ', 10); 53 printbuf_memset(pb, -1, ' ', 10); 54 printbuf_memset(pb, -1, ' ', 20); 55 printf("Buffer length: %d\n", printbuf_length(pb)); 56 57 // No length change should occur 58 printbuf_memset(pb, 0, 'x', 30); 59 printf("Buffer length: %d\n", printbuf_length(pb)); 60 61 // This should extend it by one. 62 printbuf_memset(pb, 0, 'x', printbuf_length(pb) + 1); 63 printf("Buffer length: %d\n", printbuf_length(pb)); 64 65 printbuf_free(pb); 66 printf("%s: end test\n", __func__); 67 } 68 69 static void test_printbuf_memappend(int *before_resize); 70 static void test_printbuf_memappend(int *before_resize) 71 { 72 struct printbuf *pb; 73 int initial_size; 74 75 printf("%s: starting test\n", __func__); 76 pb = printbuf_new(); 77 printf("Buffer length: %d\n", printbuf_length(pb)); 78 79 initial_size = pb->size; 80 81 while (pb->size == initial_size) 82 { 83 printbuf_memappend_fast(pb, "x", 1); 84 } 85 *before_resize = printbuf_length(pb) - 1; 86 printf("Appended %d bytes for resize: [%s]\n", *before_resize + 1, pb->buf); 87 88 printbuf_reset(pb); 89 printbuf_memappend_fast(pb, "bluexyz123", 3); 90 printf("Partial append: %d, [%s]\n", printbuf_length(pb), pb->buf); 91 92 char with_nulls[] = {'a', 'b', '\0', 'c'}; 93 printbuf_reset(pb); 94 printbuf_memappend_fast(pb, with_nulls, (int)sizeof(with_nulls)); 95 printf("With embedded \\0 character: %d, [%s]\n", printbuf_length(pb), pb->buf); 96 97 printbuf_free(pb); 98 pb = printbuf_new(); 99 char *data = malloc(*before_resize); 100 memset(data, 'X', *before_resize); 101 printbuf_memappend_fast(pb, data, *before_resize); 102 printf("Append to just before resize: %d, [%s]\n", printbuf_length(pb), pb->buf); 103 104 free(data); 105 printbuf_free(pb); 106 107 pb = printbuf_new(); 108 data = malloc(*before_resize + 1); 109 memset(data, 'X', *before_resize + 1); 110 printbuf_memappend_fast(pb, data, *before_resize + 1); 111 printf("Append to just after resize: %d, [%s]\n", printbuf_length(pb), pb->buf); 112 113 free(data); 114 printbuf_free(pb); 115 116 #define SA_TEST_STR "XXXXXXXXXXXXXXXX" 117 pb = printbuf_new(); 118 printbuf_strappend(pb, SA_TEST_STR); 119 printf("Buffer size after printbuf_strappend(): %d, [%s]\n", printbuf_length(pb), pb->buf); 120 printbuf_free(pb); 121 #undef SA_TEST_STR 122 123 printf("%s: end test\n", __func__); 124 } 125 126 static void test_sprintbuf(int before_resize); 127 static void test_sprintbuf(int before_resize) 128 { 129 struct printbuf *pb; 130 const char *max_char = 131 "if string is greater than stack buffer, then use dynamic string" 132 " with vasprintf. Note: some implementation of vsnprintf return -1 " 133 " if output is truncated whereas some return the number of bytes that " 134 " would have been written - this code handles both cases."; 135 136 printf("%s: starting test\n", __func__); 137 pb = printbuf_new(); 138 printf("Buffer length: %d\n", printbuf_length(pb)); 139 140 char *data = malloc(before_resize + 1 + 1); 141 memset(data, 'X', before_resize + 1 + 1); 142 data[before_resize + 1] = '\0'; 143 sprintbuf(pb, "%s", data); 144 free(data); 145 printf("sprintbuf to just after resize(%d+1): %d, [%s], strlen(buf)=%d\n", before_resize, 146 printbuf_length(pb), pb->buf, (int)strlen(pb->buf)); 147 148 printbuf_reset(pb); 149 sprintbuf(pb, "plain"); 150 printf("%d, [%s]\n", printbuf_length(pb), pb->buf); 151 152 sprintbuf(pb, "%d", 1); 153 printf("%d, [%s]\n", printbuf_length(pb), pb->buf); 154 155 sprintbuf(pb, "%d", INT_MAX); 156 printf("%d, [%s]\n", printbuf_length(pb), pb->buf); 157 158 sprintbuf(pb, "%d", INT_MIN); 159 printf("%d, [%s]\n", printbuf_length(pb), pb->buf); 160 161 sprintbuf(pb, "%s", "%s"); 162 printf("%d, [%s]\n", printbuf_length(pb), pb->buf); 163 164 sprintbuf(pb, max_char); 165 printf("%d, [%s]\n", printbuf_length(pb), pb->buf); 166 printbuf_free(pb); 167 printf("%s: end test\n", __func__); 168 } 169 170 int main(int argc, char **argv) 171 { 172 int before_resize = 0; 173 174 MC_SET_DEBUG(1); 175 176 test_basic_printbuf_memset(); 177 printf("========================================\n"); 178 test_printbuf_memset_length(); 179 printf("========================================\n"); 180 test_printbuf_memappend(&before_resize); 181 printf("========================================\n"); 182 test_sprintbuf(before_resize); 183 printf("========================================\n"); 184 185 return 0; 186 } 187
This page was automatically generated by LXR 0.3.1. • OpenWrt