package gobplustree import ( "bytes" "os" "testing" ) // TestNewBlockFile tests the creation of a new BlockFile instance. func TestNewBlockFile(t *testing.T) { bf, err := NewBlockFile("test.db", 100) if err != nil { t.Fatalf("Failed to create BlockFile: %v", err) } if bf.FileName != "test.db" { t.Errorf("Expected filename 'test.db', got '%s'", bf.FileName) } if bf.PageCount != 100 { t.Errorf("Expected PageCount 100, got %d", bf.PageCount) } if bf.file != nil { t.Errorf("Expected file to be nil, got %v", bf.file) } } // TestBlockFileOpenAndClose tests opening and closing a BlockFile. func TestBlockFileOpenAndClose(t *testing.T) { filename := "test_open.db" bf, err := NewBlockFile(filename, InitPageCount) if err != nil { t.Fatalf("Failed to create BlockFile: %v", err) } // Test opening the file err = bf.Open() if err != nil { t.Fatalf("Failed to open BlockFile: %v", err) } // Check that file was created if _, err := os.Stat(filename); os.IsNotExist(err) { t.Error("File was not created") } // Test closing the file err = bf.Close() if err != nil { t.Errorf("Failed to close BlockFile: %v", err) } // Clean up os.Remove(filename) } // TestBlockFileReadWrite tests reading and writing pages to a BlockFile. func TestBlockFileReadWrite(t *testing.T) { filename := "test_rw.db" bf, err := NewBlockFile(filename, 10) if err != nil { t.Fatalf("Failed to create BlockFile: %v", err) } err = bf.Open() if err != nil { t.Fatalf("Failed to open BlockFile: %v", err) } // Test writing data testData := []byte("Hello, BlockFile!") err = bf.Write(0, testData) if err != nil { t.Fatalf("Failed to write to BlockFile: %v", err) } // Pad testData to full page size for comparison paddedTestData := make([]byte, BlockFilePageSize) copy(paddedTestData, testData) // Test reading data data, err := bf.Read(0) if err != nil { t.Fatalf("Failed to read from BlockFile: %v", err) } // Check that we got the data we wrote if !bytes.Equal(data, paddedTestData) { t.Errorf("Read data does not match written data. Expected: %s, Got: %s", string(testData), string(data[:len(testData)])) } // Test reading a non-existent page _, err = bf.Read(100) if err != ErrPageIdOutOfRange { t.Errorf("Expected ErrPageIdOutOfRange, got %v", err) } // Test writing to a page that requires enlarging the file longData := make([]byte, 100) for i := range longData { longData[i] = byte(i % 256) } err = bf.Write(15, longData) if err != nil { t.Fatalf("Failed to write to page requiring file enlargement: %v", err) } // Read the data back readData, err := bf.Read(15) if err != nil { t.Fatalf("Failed to read enlarged page: %v", err) } if !bytes.Equal(readData[:len(longData)], longData) { t.Error("Read data from enlarged page does not match written data") } err = bf.Close() if err != nil { t.Errorf("Failed to close BlockFile: %v", err) } // Clean up os.Remove(filename) } // TestBlockFileEnlarge tests the internal enlarge method. func TestBlockFileEnlarge(t *testing.T) { filename := "test_enlarge.db" bf, err := NewBlockFile(filename, 5) if err != nil { t.Fatalf("Failed to create BlockFile: %v", err) } err = bf.Open() if err != nil { t.Fatalf("Failed to open BlockFile: %v", err) } initialPageCount := bf.PageCount // Manually call enlarge err = bf.enlarge(10) if err != nil { t.Fatalf("Failed to enlarge BlockFile: %v", err) } if bf.PageCount != initialPageCount+10 { t.Errorf("Expected PageCount to be %d, got %d", initialPageCount+10, bf.PageCount) } err = bf.Close() if err != nil { t.Errorf("Failed to close BlockFile: %v", err) } // Clean up os.Remove(filename) }