抄自《從零開始的JSON庫教程》,先mark一下,之後再慢慢研究。json
======== 引用分割線 ========函數
在 Linux、OS X 下,咱們能夠使用 valgrind 工具(用 apt-get install valgrind、 brew install valgrind)。咱們徹底不用修改代碼,只要在命令行執行:工具
$ valgrind --leak-check=full ./leptjson_test
$ valgrind --leak-check=full ./leptjson_test ==22078== Memcheck, a memory error detector ==22078== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al. ==22078== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info ==22078== Command: ./leptjson_test ==22078== --22078-- run: /usr/bin/dsymutil "./leptjson_test" 160/160 (100.00%) passed ==22078== ==22078== HEAP SUMMARY: ==22078== in use at exit: 27,728 bytes in 209 blocks ==22078== total heap usage: 301 allocs, 92 frees, 34,966 bytes allocated ==22078== ==22078== 2 bytes in 1 blocks are definitely lost in loss record 1 of 79 ==22078== at 0x100012EBB: malloc (in /usr/local/Cellar/valgrind/3.11.0/lib/valgrind/vgpreload_memcheck-amd64-darwin.so) ==22078== by 0x100008F36: lept_set_string (leptjson.c:208) ==22078== by 0x100008415: test_access_boolean (test.c:187) ==22078== by 0x100001849: test_parse (test.c:229) ==22078== by 0x1000017A3: main (test.c:235) ==22078==
它發現了在 test_access_boolean() 中,由 lept_set_string() 分配的 2 個字節("a")泄漏了。單元測試
Valgrind 還有不少功能,例如能夠發現未初始化變量。咱們若在應用程序或測試程序中,忘了調用 lept_init(&v),那麼v.type 的值沒被初始化,其值是不肯定的(indeterministic),一些函數若是讀取那個值就會出現問題:測試
static void test_access_boolean() { lept_value v; /* lept_init(&v); */ lept_set_string(&v, "a", 1); ... }
這種錯誤有時候測試時能正確運行(恰好 v.type 被設爲 0),使咱們誤覺得程序正確,而在發佈後一些機器上卻可能崩潰。這種誤覺得正確的假像是很危險的,咱們可利用 valgrind 能自動測出來:spa
$ valgrind --leak-check=full ./leptjson_test ... ==22174== Conditional jump or move depends on uninitialised value(s) ==22174== at 0x100008B5D: lept_free (leptjson.c:164) ==22174== by 0x100008F26: lept_set_string (leptjson.c:207) ==22174== by 0x1000083FE: test_access_boolean (test.c:187) ==22174== by 0x100001839: test_parse (test.c:229) ==22174== by 0x100001793: main (test.c:235) ==22174==
它發現 lept_free() 中依靠了一個未初始化的值來跳轉,就是 v.type,而錯誤是沿自 test_access_boolean()。命令行
編寫單元測試時,應考慮哪些執行次序會有機會出錯,例如內存相關的錯誤。而後咱們能夠利用 TDD 的步驟,先令測試失敗(之內存工具檢測),修正代碼,再確認測試是否成功。code