github.com/zuoyebang/bitalosdb@v1.1.1-0.20240516111551-79a8c4d8ce20/internal/base/filenames_test.go (about)

     1  // Copyright 2021 The Bitalosdb author(hustxrb@163.com) and other contributors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package base
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/zuoyebang/bitalosdb/internal/vfs"
    21  )
    22  
    23  func TestParseFilename(t *testing.T) {
    24  	testCases := map[string]bool{
    25  		"000000.log":             true,
    26  		"000000.log.zip":         false,
    27  		"000000..log":            false,
    28  		"a000000.log":            false,
    29  		"abcdef.log":             false,
    30  		"000001ldb":              false,
    31  		"000001.sst":             true,
    32  		"CURRENT":                true,
    33  		"CURRaNT":                false,
    34  		"LOCK":                   true,
    35  		"xLOCK":                  false,
    36  		"x.LOCK":                 false,
    37  		"MANIFEST":               false,
    38  		"MANIFEST123456":         false,
    39  		"MANIFEST-":              false,
    40  		"MANIFEST-123456":        true,
    41  		"MANIFEST-123456.doc":    false,
    42  		"OPTIONS":                false,
    43  		"OPTIONS123456":          false,
    44  		"OPTIONS-":               false,
    45  		"OPTIONS-123456":         true,
    46  		"OPTIONS-123456.doc":     false,
    47  		"CURRENT.123456":         false,
    48  		"CURRENT.dbtmp":          false,
    49  		"CURRENT.123456.dbtmp":   true,
    50  		"temporary.123456.dbtmp": true,
    51  	}
    52  	fs := vfs.NewMem()
    53  	for tc, want := range testCases {
    54  		_, _, got := ParseFilename(fs, fs.PathJoin("foo", tc))
    55  		if got != want {
    56  			t.Errorf("%q: got %v, want %v", tc, got, want)
    57  		}
    58  	}
    59  }
    60  
    61  func TestFilenameRoundTrip(t *testing.T) {
    62  	testCases := map[FileType]bool{
    63  		FileTypeCurrent:  false,
    64  		FileTypeLock:     false,
    65  		FileTypeLog:      true,
    66  		FileTypeManifest: true,
    67  	}
    68  	fs := vfs.NewMem()
    69  	for fileType, numbered := range testCases {
    70  		fileNums := []FileNum{0}
    71  		if numbered {
    72  			fileNums = []FileNum{0, 1, 2, 3, 10, 42, 99, 1001}
    73  		}
    74  		for _, fileNum := range fileNums {
    75  			filename := MakeFilepath(fs, "foo", fileType, fileNum)
    76  			gotFT, gotFN, gotOK := ParseFilename(fs, filename)
    77  			if !gotOK {
    78  				t.Errorf("could not parse %q", filename)
    79  				continue
    80  			}
    81  			if gotFT != fileType || gotFN != fileNum {
    82  				t.Errorf("filename=%q: got %v, %v, want %v, %v", filename, gotFT, gotFN, fileType, fileNum)
    83  				continue
    84  			}
    85  		}
    86  	}
    87  }