最近在写红黑树,为了保证算法写的正确性,写了个测试程序,循环进行插入删除,然后和内核实现对比。为了查看测试程序对代码的覆盖度,可以使用gcov。
首先,使用下述选项,编译程序:
-fprofile-arcs -ftest-coverage -fPIC -O0
注意编译和链接都需要使用上述选项:
cc -O0 -fprofile-arcs -ftest-coverage -fPIC -O0 -c -o test.o test.c
cc -O0 -fprofile-arcs -ftest-coverage -fPIC -O0 -c -o rbtree.o ../rbtree.c
cc -O0 -fprofile-arcs -ftest-coverage -fPIC -O0 -c -o rbtree-kernel-tst.o rbtree-kernel-tst.c
cc -O0 -fprofile-arcs -ftest-coverage -fPIC -O0 -c -o rbtree-kernel.o rbtree-kernel.c
cc -O0 -fprofile-arcs -ftest-coverage -fPIC -O0 -o a.out test.o rbtree.o rbtree-kernel-tst.o rbtree-kernel.o
编译后,会生成一些.gcno文件:
$ ls
a.out rbtree.gcno rbtree-kernel.gcno rbtree-kernel.o rbtree-kernel-tst.gcno rbtree.o test.gcno
Makefile rbtree-kernel.c rbtree-kernel.h rbtree-kernel-tst.c rbtree-kernel-tst.o test.c test.o
然后执行程序,执行完毕后,可以生成一些.gcda文件。
$ ./a.out
$ ls
a.out rbtree.gcno rbtree-kernel.gcno rbtree-kernel-tst.c rbtree-kernel-tst.o test.gcda
Makefile rbtree-kernel.c rbtree-kernel.h rbtree-kernel-tst.gcda rbtree.o test.gcno
rbtree.gcda rbtree-kernel.gcda rbtree-kernel.o rbtree-kernel-tst.gcno test.c test.o
执行gcov生成覆盖报告:
$ gcov -f rbtree.gcda
Function 'rb_delete'
Lines executed:98.26% of 115
Function 'rb_insert'
Lines executed:97.06% of 34
Function 'rotate'
Lines executed:100.00% of 47
Function 'replace'
Lines executed:100.00% of 9
Function 'rb_find'
Lines executed:0.00% of 10
Function 'rb_init'
Lines executed:100.00% of 4
File '../rbtree.c'
Lines executed:94.06% of 219
Creating 'rbtree.c.gcov'
Lines executed:94.06% of 219
上述,生成了rbtree.c.gcov,这个文件显示了每一行的执行次数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 | $ cat rbtree.c.gcov -: 0:Source:../rbtree.c -: 0:Graph:rbtree.gcno -: 0:Data:rbtree.gcda -: 0:Runs:1 -: 1:#include "rbtree.h" -: 2:#include <stddef.h> -: 3: 50: 4: void rb_init( struct rb_tree *tree, int (*cmp)( struct rb_node *, struct rb_node *)) -: 5:{ 50: 6: tree->root = NULL; 50: 7: tree->cmp = cmp; 50: 8:} -: 9: #####: 10: struct rb_node *rb_find( struct rb_tree *tree, struct rb_node *key) -: 11:{ #####: 12: struct rb_node *node = tree->root; -: 13: int ret; -: 14: #####: 15: while (node) { #####: 16: ret = tree->cmp(node, key); #####: 17: if (ret < 0) #####: 18: node = node->left; #####: 19: else if (ret > 0) #####: 20: node = node->right; -: 21: else #####: 22: return node; -: 23: } -: 24: #####: 25: return NULL; -: 26:} -: 27: 10291: 28: static inline void replace( struct rb_tree *tree, struct rb_node *old, struct rb_node * new ) -: 29:{ 10291: 30: if (old == tree->root) 428: 31: tree->root = new ; 9863: 32: else if (old == old->parent->left) 5002: 33: old->parent->left = new ; -: 34: else 4861: 35: old->parent->right = new ; 10291: 36: if ( new ) 6347: 37: new ->parent = old->parent; 10291: 38:} -: 39: 1894: 40: static void inline rotate( struct rb_tree *tree, struct rb_node *x) -: 41:{ 1894: 42: struct rb_node *p = x->parent; 1894: 43: struct rb_node *g = p->parent; -: 44: 1894: 45: if (p == g->left) { -: 46: // Left left case 882: 47: if (x == p->left) { 411: 48: p->color = RB_BLACK; 411: 49: g->color = RB_RED; 411: 50: g->left = p->right; 411: 51: if (p->right) 103: 52: p->right->parent = g; 411: 53: p->right = g; 411: 54: replace(tree, g, p); 411: 55: g->parent = p; -: 56: } -: 57: -: 58: // Left right case -: 59: else { 471: 60: x->color = RB_BLACK; 471: 61: g->color = RB_RED; 471: 62: p->right = x->left; 471: 63: if (x->left) 111: 64: x->left->parent = p; 471: 65: g->left = x->right; 471: 66: if (x->right) 111: 67: x->right->parent = g; 471: 68: x->left = p; 471: 69: x->right = g; 471: 70: replace(tree, g, x); 471: 71: p->parent = g->parent = x; -: 72: } -: 73: } -: 74: -: 75: else { -: 76: // Right left case 1012: 77: if (x == p->left) { 471: 78: x->color = RB_BLACK; 471: 79: g->color = RB_RED; 471: 80: p->left = x->right; 471: 81: if (x->right) 117: 82: x->right->parent = p; 471: 83: g->right = x->left; 471: 84: if (x->left) 117: 85: x->left->parent = g; 471: 86: x->left = g; 471: 87: x->right = p; 471: 88: replace(tree, g, x); 471: 89: p->parent = g->parent = x; -: 90: } -: 91: -: 92: // Right right case -: 93: else { 541: 94: p->color = RB_BLACK; 541: 95: g->color = RB_RED; 541: 96: g->right = p->left; 541: 97: if (p->left) 150: 98: p->left->parent = g; 541: 99: p->left = g; 541: 100: replace(tree, g, p); 541: 101: g->parent = p; -: 102: } -: 103: } 1894: 104:} -: 105: 5000: 106: int rb_insert( struct rb_tree *tree, struct rb_node *node) -: 107:{ 5000: 108: struct rb_node **tmp = &tree->root; 5000: 109: struct rb_node *parent = NULL; -: 110: int ret; -: 111: -: 112: // make the colour of newly inserted nodes as RED 5000: 113: node->color = RB_RED; 5000: 114: node->left = NULL; 5000: 115: node->right = NULL; -: 116: -: 117: // Perform standard BST insertion 32257: 118: while (*tmp) { 27257: 119: parent = *tmp; 27257: 120: ret = tree->cmp(parent, node); 27257: 121: if (ret < 0) 12843: 122: tmp = &parent->left; 14414: 123: else if (ret > 0) 14414: 124: tmp = &parent->right; -: 125: else #####: 126: return 0; -: 127: } 5000: 128: *tmp = node; 5000: 129: node->parent = parent; -: 130: -: 131: // Condition 1, If x is the root, change the colour of x as BLACK 5000: 132: if (tree->root == node) { 50: 133: node->color = RB_BLACK; 50: 134: return 1; -: 135: } -: 136: -: 137: // Condition 2, If parent is BLACK, insert done -: 138: -: 139: // Condition 3, parent is red 7149: 140: while (parent->color == RB_RED) { -: 141: struct rb_node *uncle, *grandpa; -: 142: 4245: 143: grandpa = parent->parent; 4245: 144: uncle = (parent == grandpa->left) ? grandpa->right : grandpa->left; -: 145: -: 146: // 3.1 uncle is red 4245: 147: if (uncle && uncle->color == RB_RED) { -: 148: -: 149: // change parent and uncle to BLACK 2351: 150: parent->color = RB_BLACK; 2351: 151: uncle->color = RB_BLACK; -: 152: -: 153: // 3.1.1 grandpa is root, insert done 2351: 154: if (grandpa == tree->root) 152: 155: break ; -: 156: -: 157: // 3.1.2 grandpa is not root, change its color to RED 2199: 158: grandpa->color = RB_RED; -: 159: -: 160: // set x to grandpa, and continue to check 2199: 161: node = grandpa; 2199: 162: parent = node->parent; -: 163: 2199: 164: continue ; -: 165: } -: 166: -: 167: // 3.2 uncle is BLACK, we need recoloring and rotating 1894: 168: rotate(tree, node); -: 169: -: 170: // after recoloring and rotating, the tree is balanced 1894: 171: break ; -: 172: } -: 173: 4950: 174: return 1; -: 175:} -: 176: 5000: 177: void rb_delete( struct rb_tree *tree, struct rb_node *x) -: 178:{ -: 179: struct rb_node *s, *p; -: 180: struct rb_node *m, *n; -: 181: -: 182: // Conditon 3, the deleted node has 2 childs 5000: 183: if (x->left && x->right) -: 184: { 2101: 185: int color = x->color; -: 186: 2101: 187: m = x->right; // m is right child 2101: 188: n = m; 4026: 189: while (n->left) 1925: 190: n = n->left; // n is leftmost node -: 191: 2101: 192: x->color = n->color; 2101: 193: n->color = color; -: 194: 2101: 195: n->left = x->left; 2101: 196: if (x->left) 2101: 197: x->left->parent = n; 2101: 198: x->left = NULL; 2101: 199: x->right = n->right; -: 200: 2101: 201: n->right = m; 2101: 202: m->parent = n; -: 203: 2101: 204: m = n->parent; // cache leftmost's parent to m 2101: 205: replace(tree, x, n); -: 206: 2101: 207: if (n->right == n) { // leftmost is x's right child 919: 208: n->right = x; 919: 209: x->parent = n; -: 210: } -: 211: else { 1182: 212: m->left = x; 1182: 213: x->parent = m; -: 214: } -: 215: -: 216: // fallthrough to process leftmost deletion -: 217: } -: 218: -: 219: // Condition 2, has only one child 5000: 220: if (x->left || x->right) -: 221: { 1056: 222: m = x->left ? : x->right; 1056: 223: replace(tree, x, m); -: 224: -: 225: // if any is red, delete done 1056: 226: if (x->color == RB_RED || m->color == RB_RED) { 1056: 227: m->color = RB_BLACK; 1056: 228: return ; -: 229: } -: 230: -: 231: // if both is black, fallthrough to fixup #####: 232: p = m; #####: 233: s = NULL; -: 234: } -: 235: -: 236: // Condition 1, no child -: 237: else { 3944: 238: p = x->parent; 3944: 239: if (p) 3894: 240: s = (x == p->left) ? p->right : p->left; -: 241: 3944: 242: replace(tree, x, NULL); -: 243: -: 244: // 1.1 the deleted node is red, delete done 3944: 245: if (x->color == RB_RED) 1413: 246: return ; -: 247: -: 248: // 1.2 the deleted node is black, fallthrough to fixup -: 249: } -: 250: -: 251: // rotating and recoloring 3849: 252: while (p) { -: 253: -: 254: // sibling is black 3647: 255: if (!s || s->color == RB_BLACK) -: 256: { -: 257: // b) its both children are black 3379: 258: if (!s || ((!s->left || s->left->color == RB_BLACK) && 2788: 259: (!s->right || s->right->color == RB_BLACK))) -: 260: { 2351: 261: if (s) 2351: 262: s->color = RB_RED; -: 263: 2351: 264: if (p->color == RB_RED) { 1301: 265: p->color = RB_BLACK; 1301: 266: return ; -: 267: } -: 268: 1050: 269: if (p->parent) 898: 270: s = (p == p->parent->left) ? p->parent->right : p->parent->left; 1050: 271: p = p->parent; 1050: 272: continue ; -: 273: } -: 274: -: 275: // a) at least one child is red 1028: 276: if (s == p->right) { 509: 277: if (s->right && s->right->color == RB_RED) { 246: 278: s->right->color = RB_BLACK; 246: 279: s->color = p->color; 246: 280: p->color = RB_BLACK; 246: 281: p->right = s->left; 246: 282: if (s->left) 103: 283: s->left->parent = p; 246: 284: s->left = p; 246: 285: replace(tree, p, s); 246: 286: p->parent = s; -: 287: } -: 288: else { 263: 289: m = s->left; 263: 290: m->color = p->color; 263: 291: p->color = RB_BLACK; 263: 292: replace(tree, p, m); 263: 293: p->right = m->left; 263: 294: if (m->left) 57: 295: m->left->parent = p; 263: 296: s->left = m->right; 263: 297: if (m->right) 57: 298: m->right->parent = s; 263: 299: m->left = p; 263: 300: p->parent = m; 263: 301: m->right = s; 263: 302: s->parent = m; -: 303: } -: 304: } -: 305: else { 519: 306: if (s->left && s->left->color == RB_RED) { 274: 307: s->left->color = RB_BLACK; 274: 308: s->color = p->color; 274: 309: p->color = RB_BLACK; 274: 310: p->left = s->right; 274: 311: if (s->right) 119: 312: s->right->parent = p; 274: 313: s->right = p; 274: 314: replace(tree, p, s); 274: 315: p->parent = s; -: 316: } -: 317: else { 245: 318: m = s->right; 245: 319: m->color = p->color; 245: 320: p->color = RB_BLACK; 245: 321: replace(tree, p, m); 245: 322: p->left = m->right; 245: 323: if (m->right) 40: 324: m->right->parent = p; 245: 325: s->right = m->left; 245: 326: if (m->left) 40: 327: m->left->parent = s; 245: 328: m->right = p; 245: 329: p->parent = m; 245: 330: m->left = s; 245: 331: s->parent = m; -: 332: } -: 333: } -: 334: 1028: 335: return ; -: 336: } -: 337: -: 338: else { -: 339: // c) sibling is red 268: 340: replace(tree, p, s); -: 341: 268: 342: p->parent = s; 268: 343: p->color = RB_RED; 268: 344: s->color = RB_BLACK; -: 345: 268: 346: if (s == p->right) { 115: 347: p->right = s->left; 115: 348: if (s->left) 115: 349: s->left->parent = p; 115: 350: s->left = p; 115: 351: s = p->right; // update p s and continue -: 352: } -: 353: else { 153: 354: p->left = s->right; 153: 355: if (s->right) 153: 356: s->right->parent = p; 153: 357: s->right = p; 153: 358: s = p->left; -: 359: } -: 360: } -: 361: } -: 362:} -: 363: |
可以看到,rb_delete 232/233行没有覆盖到。但是分析发现,实际是不存在这个场景。
参考:
gcov—a Test Coverage Program. https://gcc.gnu.org/onlinedocs/gcc/Gcov.html
Gcovr User Guide. https://gcovr.github.io/guide.html