How to test the equivalence of maps in Golang?

前端 未结 8 1454
情歌与酒
情歌与酒 2021-01-30 00:55

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 s         


        
8条回答
  •  我在风中等你
    2021-01-30 01:07

    This is what I would do (untested code):

    func eq(a, b map[string]int) bool {
            if len(a) != len(b) {
                    return false
            }
    
            for k, v := range a {
                    if w, ok := b[k]; !ok || v != w {
                            return false
                    }
            }
    
            return true
    }
    

提交回复
热议问题