github.com/petermattis/pebble@v0.0.0-20190905164901-ab51a2166067/internal/base/filenames_test.go (about) 1 // Copyright 2012 The LevelDB-Go and Pebble Authors. All rights reserved. Use 2 // of this source code is governed by a BSD-style license that can be found in 3 // the LICENSE file. 4 5 package base 6 7 import ( 8 "path/filepath" 9 "testing" 10 ) 11 12 func TestParseFilename(t *testing.T) { 13 testCases := map[string]bool{ 14 "000000.log": true, 15 "000000.log.zip": false, 16 "000000..log": false, 17 "a000000.log": false, 18 "abcdef.log": false, 19 "000001ldb": false, 20 "000001.sst": true, 21 "CURRENT": true, 22 "CURRaNT": false, 23 "LOCK": true, 24 "xLOCK": false, 25 "x.LOCK": false, 26 "MANIFEST": false, 27 "MANIFEST123456": false, 28 "MANIFEST-": false, 29 "MANIFEST-123456": true, 30 "MANIFEST-123456.doc": false, 31 "OPTIONS": false, 32 "OPTIONS123456": false, 33 "OPTIONS-": false, 34 "OPTIONS-123456": true, 35 "OPTIONS-123456.doc": false, 36 } 37 for tc, want := range testCases { 38 _, _, got := ParseFilename(filepath.Join("foo", tc)) 39 if got != want { 40 t.Errorf("%q: got %v, want %v", tc, got, want) 41 } 42 } 43 } 44 45 func TestFilenameRoundTrip(t *testing.T) { 46 testCases := map[FileType]bool{ 47 // CURRENT and LOCK files aren't numbered. 48 FileTypeCurrent: false, 49 FileTypeLock: false, 50 // The remaining file types are numbered. 51 FileTypeLog: true, 52 FileTypeManifest: true, 53 FileTypeTable: true, 54 FileTypeOptions: true, 55 } 56 for fileType, numbered := range testCases { 57 fileNums := []uint64{0} 58 if numbered { 59 fileNums = []uint64{0, 1, 2, 3, 10, 42, 99, 1001} 60 } 61 for _, fileNum := range fileNums { 62 filename := MakeFilename("foo", fileType, fileNum) 63 gotFT, gotFN, gotOK := ParseFilename(filename) 64 if !gotOK { 65 t.Errorf("could not parse %q", filename) 66 continue 67 } 68 if gotFT != fileType || gotFN != fileNum { 69 t.Errorf("filename=%q: got %v, %v, want %v, %v", filename, gotFT, gotFN, fileType, fileNum) 70 continue 71 } 72 } 73 } 74 }