func makeGraphData(t testing.TB, n int) graph.Output { data := graph.Output{} if n > 0 { data.Defs = make([]*graph.Def, n) data.Refs = make([]*graph.Ref, n) } for i := 0; i < n; i++ { data.Defs[i] = &graph.Def{ DefKey: graph.DefKey{Path: fmt.Sprintf("def-path-%d", i)}, Name: fmt.Sprintf("def-name-%d", i), Kind: "mykind", DefStart: uint32((i % 53) * 37), DefEnd: uint32((i%53)*37 + (i % 20)), File: fmt.Sprintf("dir%d/subdir%d/subsubdir%d/file-%d.foo", i%5, i%3, i%7, i%5), Exported: i%5 == 0, Local: i%3 == 0, Data: []byte(`"` + strings.Repeat("abcd", 50) + `"`), } data.Refs[i] = &graph.Ref{ DefPath: fmt.Sprintf("ref-path-%d", i), Def: i%5 == 0, Start: uint32((i % 51) * 39), End: uint32((i%51)*37 + (int(i) % 18)), File: fmt.Sprintf("dir%d/subdir%d/subsubdir%d/file-%d.foo", i%3, i%5, i%7, i%5), } if i%3 == 0 { data.Refs[i].DefUnit = fmt.Sprintf("def-unit-%d", i%17) data.Refs[i].DefUnitType = fmt.Sprintf("def-unit-type-%d", i%3) if i%7 == 0 { data.Refs[i].DefRepo = fmt.Sprintf("def-repo-%d", i%13) } } } return data }
func testUnitStore_Refs_ByDef(t *testing.T, us UnitStoreImporter) { refsByDef := map[string][]*graph.Ref{ "p1": { {File: "f1", Start: 0, End: 5}, }, "p2": { {File: "f1", Start: 0, End: 5}, {File: "f2", Start: 5, End: 10}, }, "p3": { {File: "f3", Start: 0, End: 5}, {File: "f1", Start: 5, End: 10}, {File: "f1", Start: 10, End: 15}, }, } var data graph.Output for defPath, refs := range refsByDef { for _, ref := range refs { ref.DefPath = defPath } data.Refs = append(data.Refs, refs...) } if err := us.Import(data); err != nil { t.Errorf("%s: Import(data): %s", us, err) } for defPath, wantRefs := range refsByDef { c_defRefsIndex_getByDef = 0 refs, err := us.Refs(ByRefDef(graph.RefDefKey{DefPath: defPath})) if err != nil { t.Fatalf("%s: Refs(ByDefs %s): %s", us, defPath, err) } sort.Sort(refsByFileStartEnd(refs)) sort.Sort(refsByFileStartEnd(wantRefs)) if want := wantRefs; !reflect.DeepEqual(refs, want) { t.Errorf("%s: Refs(ByDefs %s): got refs %v, want %v", us, defPath, refs, want) } if isIndexedStore(us) { if want := 1; c_defRefsIndex_getByDef != want { t.Errorf("%s: Refs(ByDefs %s): got %d index hits, want %d", us, defPath, c_defRefsIndex_getByDef, want) } } } }
func testUnitStore_Refs_ByFiles(t *testing.T, us UnitStoreImporter) { refsByFile := map[string][]*graph.Ref{ "f1": { {DefPath: "p1", Start: 0, End: 5}, }, "f2": { {DefPath: "p1", Start: 0, End: 5}, {DefPath: "p2", Start: 5, End: 10}, }, "f3": { {DefPath: "p1", Start: 0, End: 5}, {DefPath: "p2", Start: 5, End: 10}, {DefPath: "p3", Start: 10, End: 15}, }, } var data graph.Output for file, refs := range refsByFile { for _, ref := range refs { ref.File = file } data.Refs = append(data.Refs, refs...) } if err := us.Import(data); err != nil { t.Errorf("%s: Import(data): %s", us, err) } for file, wantRefs := range refsByFile { c_refFileIndex_getByFile = 0 refs, err := us.Refs(ByFiles(file)) if err != nil { t.Fatalf("%s: Refs(ByFiles %s): %s", us, file, err) } sort.Sort(refsByFileStartEnd(refs)) sort.Sort(refsByFileStartEnd(wantRefs)) if want := wantRefs; !reflect.DeepEqual(refs, want) { t.Errorf("%s: Refs(ByFiles %s): got refs %v, want %v", us, file, refs, want) } if isIndexedStore(us) { if want := 1; c_refFileIndex_getByFile != want { t.Errorf("%s: Refs(ByFiles %s): got %d index hits, want %d", us, file, c_refFileIndex_getByFile, want) } } } }
func testTreeStore_Refs_ByFiles(t *testing.T, ts TreeStoreImporter) { refsByUnitByFile := map[string]map[string][]*graph.Ref{ "u1": { "f1": { {DefPath: "p1", Start: 0, End: 5}, }, "f2": { {DefPath: "p1", Start: 0, End: 5}, {DefPath: "p2", Start: 5, End: 10}, }, }, "u2": { "f1": { {DefPath: "p1", Start: 5, End: 10}, }, }, } refsByFile := map[string][]*graph.Ref{} for unitName, refsByFile0 := range refsByUnitByFile { u := &unit.SourceUnit{Type: "t", Name: unitName} var data graph.Output for file, refs := range refsByFile0 { u.Files = append(u.Files, file) for _, ref := range refs { ref.File = file } data.Refs = append(data.Refs, refs...) refsByFile[file] = append(refsByFile[file], refs...) } if err := ts.Import(u, data); err != nil { t.Errorf("%s: Import(%v, data): %s", ts, u, err) } } if ts, ok := ts.(TreeIndexer); ok { if err := ts.Index(); err != nil { t.Fatalf("%s: Index: %s", ts, err) } } for file, wantRefs := range refsByFile { c_unitStores_Refs_last_numUnitsQueried = 0 c_refFileIndex_getByFile = 0 refs, err := ts.Refs(ByFiles(file)) if err != nil { t.Fatalf("%s: Refs(ByFiles %s): %s", ts, file, err) } distinctRefUnits := map[string]struct{}{} for _, ref := range refs { distinctRefUnits[ref.Unit] = struct{}{} } // for test equality sort.Sort(refsByFileStartEnd(refs)) sort.Sort(refsByFileStartEnd(wantRefs)) cleanForImport(&graph.Output{Refs: refs}, "", "t", "u1") cleanForImport(&graph.Output{Refs: refs}, "", "t", "u2") if want := wantRefs; !reflect.DeepEqual(refs, want) { t.Errorf("%s: Refs(ByFiles %s): got refs %v, want %v", ts, file, refs, want) } if isIndexedStore(ts) { if want := len(distinctRefUnits); c_refFileIndex_getByFile != want { t.Errorf("%s: Refs(ByFiles %s): got %d index hits, want %d", ts, file, c_refFileIndex_getByFile, want) } if want := len(distinctRefUnits); c_unitStores_Refs_last_numUnitsQueried != want { t.Errorf("%s: Refs(ByFiles %s): got %d units queried, want %d", ts, file, c_unitStores_Refs_last_numUnitsQueried, want) } } } }