github.com/wtsi-ssg/wrstat/v4@v4.5.1/ch/prefix_test.go (about) 1 /******************************************************************************* 2 * Copyright (c) 2023 Genome Research Ltd. 3 * 4 * Authors: Michael Woolnough <mw31@sanger.ac.uk> 5 * Sendu Bala <sb10@sanger.ac.uk> 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining 8 * a copy of this software and associated documentation files (the 9 * "Software"), to deal in the Software without restriction, including 10 * without limitation the rights to use, copy, modify, merge, publish, 11 * distribute, sublicense, and/or sell copies of the Software, and to 12 * permit persons to whom the Software is furnished to do so, subject to 13 * the following conditions: 14 * 15 * The above copyright notice and this permission notice shall be included 16 * in all copies or substantial portions of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 21 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 ******************************************************************************/ 26 27 package ch 28 29 import ( 30 "path/filepath" 31 "strconv" 32 "testing" 33 34 . "github.com/smartystreets/goconvey/convey" 35 ) 36 37 const prefixBenchmarkNumPrefixes = 1000 38 39 func TestPrefixTree(t *testing.T) { 40 prefixBenchmarkPrefixes := makePrefixBenchmarkPrefixes() 41 42 Convey("pathPrefixTree longestPrefix works", t, func() { 43 tree := newPathPrefixTree() 44 45 for _, prefix := range prefixBenchmarkPrefixes { 46 tree.addDirectory(prefix) 47 } 48 49 _, found := tree.longestPrefix("/9/file.txt") 50 So(found, ShouldBeFalse) 51 52 _, found = tree.longestPrefix("/0/file.txt") 53 So(found, ShouldBeFalse) 54 55 prefix, found := tree.longestPrefix(filepath.Join(prefixBenchmarkPrefixes[0], "sub", "file.txt")) 56 So(found, ShouldBeTrue) 57 So(prefix, ShouldEqual, prefixBenchmarkPrefixes[0]) 58 59 prefix, found = tree.longestPrefix( 60 filepath.Join(prefixBenchmarkPrefixes[prefixBenchmarkNumPrefixes-1], "file.txt")) 61 So(found, ShouldBeTrue) 62 So(prefix, ShouldEqual, prefixBenchmarkPrefixes[prefixBenchmarkNumPrefixes-1]) 63 }) 64 } 65 66 func makePrefixBenchmarkPrefixes() []string { 67 prefixBenchmarkPrefixes := make([]string, 0, prefixBenchmarkNumPrefixes) 68 69 cb := func(path string) { 70 prefixBenchmarkPrefixes = append(prefixBenchmarkPrefixes, path) 71 } 72 73 makePrefixPath("/", 5, 8, cb) 74 makePrefixPath("/", 5, 6, cb) 75 makePrefixPath("/", 5, 5, cb) 76 makePrefixPath("/", 5, 4, cb) 77 makePrefixPath("/", 5, 3, cb) 78 79 return prefixBenchmarkPrefixes 80 } 81 82 func makePrefixPath(path string, max, remaining int, cb func(string)) { 83 if remaining == 0 { 84 cb(path) 85 86 return 87 } 88 89 for i := 0; i < max; i++ { 90 iDir := strconv.Itoa(i) 91 92 makePrefixPath(filepath.Join(path, iDir), max, remaining-1, cb) 93 } 94 }