github.com/cockroachdb/pebble@v0.0.0-20231214172447-ab4952c5f87b/sstable/format_test.go (about)

     1  // Copyright 2022 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 sstable
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestTableFormat_RoundTrip(t *testing.T) {
    14  	tcs := []struct {
    15  		name    string
    16  		magic   string
    17  		version uint32
    18  		want    TableFormat
    19  		wantErr string
    20  	}{
    21  		// Valid cases.
    22  		{
    23  			name:    "LevelDB",
    24  			magic:   levelDBMagic,
    25  			version: 0,
    26  			want:    TableFormatLevelDB,
    27  		},
    28  		{
    29  			name:    "RocksDBv2",
    30  			magic:   rocksDBMagic,
    31  			version: 2,
    32  			want:    TableFormatRocksDBv2,
    33  		},
    34  		{
    35  			name:    "PebbleDBv1",
    36  			magic:   pebbleDBMagic,
    37  			version: 1,
    38  			want:    TableFormatPebblev1,
    39  		},
    40  		{
    41  			name:    "PebbleDBv2",
    42  			magic:   pebbleDBMagic,
    43  			version: 2,
    44  			want:    TableFormatPebblev2,
    45  		},
    46  		{
    47  			name:    "PebbleDBv3",
    48  			magic:   pebbleDBMagic,
    49  			version: 3,
    50  			want:    TableFormatPebblev3,
    51  		},
    52  		{
    53  			name:    "PebbleDBv4",
    54  			magic:   pebbleDBMagic,
    55  			version: 4,
    56  			want:    TableFormatPebblev4,
    57  		},
    58  		// Invalid cases.
    59  		{
    60  			name:    "Invalid RocksDB version",
    61  			magic:   rocksDBMagic,
    62  			version: 1,
    63  			wantErr: "pebble/table: unsupported rocksdb format version 1",
    64  		},
    65  		{
    66  			name:    "Invalid PebbleDB version",
    67  			magic:   pebbleDBMagic,
    68  			version: 5,
    69  			wantErr: "pebble/table: unsupported pebble format version 5",
    70  		},
    71  		{
    72  			name:    "Unknown magic string",
    73  			magic:   "foo",
    74  			wantErr: "pebble/table: invalid table (bad magic number: 0x666f6f)",
    75  		},
    76  	}
    77  
    78  	for _, tc := range tcs {
    79  		t.Run(tc.name, func(t *testing.T) {
    80  			// Tuple -> TableFormat.
    81  			f, err := ParseTableFormat([]byte(tc.magic), tc.version)
    82  			if tc.wantErr != "" {
    83  				require.Error(t, err)
    84  				require.Equal(t, tc.wantErr, err.Error())
    85  				return
    86  			}
    87  			require.NoError(t, err)
    88  			require.Equal(t, tc.want, f)
    89  
    90  			// TableFormat -> Tuple.
    91  			s, v := f.AsTuple()
    92  			require.Equal(t, tc.magic, s)
    93  			require.Equal(t, tc.version, v)
    94  		})
    95  	}
    96  }