table-driven

How to test the equivalence of maps in Golang?

穿精又带淫゛_ 提交于 2019-12-03 02:42:45
问题 I have a table-driven test case like this one: func CountWords(s string) map[string]int func TestCountWords(t *testing.T) { var tests = []struct { input string want map[string]int }{ {"foo", map[string]int{"foo":1}}, {"foo bar foo", map[string]int{"foo":2,"bar":1}}, } for i, c := range tests { got := CountWords(c.input) // TODO test whether c.want == got } } I could check whether the lengths are the same and write a loop that checks if every key-value pair is the same. But then I have to

What are table-driven methods?

最后都变了- 提交于 2019-11-27 04:41:30
问题 What is a "table-driven method"? As mentioned by Bill Gates in the second Windows Vista commercial at 1:05. 回答1: Table-driven methods are schemes that allow you to look up information in a table rather than using logic statements (i.e. case, if). In simple cases, it's quicker and easier to use logic statements, but as the logic chain becomes more complex, table-driven code is simpler than complicated logic, easier to modify and more efficient. Reference: McConnell, Steve. Code Complete,