Add format test, fix dump block and bitmap bug

This commit is contained in:
Terence Boldt 2021-07-01 22:35:26 -04:00
parent d3af1d42bd
commit 07e0bacab3
3 changed files with 74 additions and 5 deletions

View File

@ -9,14 +9,23 @@ func ReadVolumeBitmap(file *os.File) []byte {
volumeHeader := parseVolumeHeader(headerBlock) volumeHeader := parseVolumeHeader(headerBlock)
bitmap := make([]byte, volumeHeader.TotalBlocks/8+1) totalBitmapBytes := volumeHeader.TotalBlocks / 8
if volumeHeader.TotalBlocks%8 > 0 {
totalBitmapBytes++
}
totalBitmapBlocks := volumeHeader.TotalBlocks / 8 / 512 bitmap := make([]byte, totalBitmapBytes)
for i := 0; i <= totalBitmapBlocks; i++ { totalBitmapBlocks := totalBitmapBytes / 512
if totalBitmapBytes%512 > 0 {
totalBitmapBlocks++
}
for i := 0; i < totalBitmapBlocks; i++ {
bitmapBlock := ReadBlock(file, i+volumeHeader.BitmapStartBlock) bitmapBlock := ReadBlock(file, i+volumeHeader.BitmapStartBlock)
for j := 0; j < 512; j++ { for j := 0; j < 512 && i*512+j < totalBitmapBytes; j++ {
bitmap[i*512+j] = bitmapBlock[j] bitmap[i*512+j] = bitmapBlock[j]
} }
} }
@ -92,6 +101,17 @@ func findFreeBlocks(volumeBitmap []byte, numberOfBlocks int) []int {
return nil return nil
} }
func getFreeBlockCount(volumeBitmap []byte, totalBlocks int) int {
freeBlockCount := 0
for i := 0; i < totalBlocks; i++ {
if checkFreeBlockInVolumeBitmap(volumeBitmap, i) {
freeBlockCount++
}
}
return freeBlockCount
}
func markBlockInVolumeBitmap(volumeBitmap []byte, blockNumber int) { func markBlockInVolumeBitmap(volumeBitmap []byte, blockNumber int) {
bitToChange := blockNumber % 8 bitToChange := blockNumber % 8
byteToChange := blockNumber / 8 byteToChange := blockNumber / 8

49
prodos/format_test.go Normal file
View File

@ -0,0 +1,49 @@
package prodos
import (
"fmt"
"os"
"testing"
)
func TestCreateVolume(t *testing.T) {
var tests = []struct {
blocks int
wantVolumeName string
wantFreeBlocks int
}{
{65535, "MAX", 65513},
{65500, "ALMOST.MAX", 65478},
{280, "FLOPPY", 273},
}
for _, tt := range tests {
testname := fmt.Sprintf("%d", tt.blocks)
t.Run(testname, func(t *testing.T) {
fileName := os.TempDir() + "test-volume.hdv"
os.Remove(fileName)
CreateVolume(fileName, tt.wantVolumeName, tt.blocks)
file, err := os.Open(fileName)
if err != nil {
t.Error(err)
}
volumeHeader, _, fileEntries := ReadDirectory(file, "")
if volumeHeader.VolumeName != tt.wantVolumeName {
t.Errorf("got volume name %s, want %s", volumeHeader.VolumeName, tt.wantVolumeName)
}
if volumeHeader.TotalBlocks != tt.blocks {
t.Errorf("got total blocks %d, want %d", volumeHeader.TotalBlocks, tt.blocks)
}
if len(fileEntries) > 0 {
t.Errorf("got files %d, want 0", len(fileEntries))
}
volumeBitmap := ReadVolumeBitmap(file)
freeBlockCount := getFreeBlockCount(volumeBitmap, tt.blocks)
if freeBlockCount != tt.wantFreeBlocks {
t.Errorf("got free blocks: %d, want %d", freeBlockCount, tt.wantFreeBlocks)
}
})
}
}

View File

@ -120,7 +120,7 @@ func DumpBlock(buffer []byte) {
} }
for j := i; j < i+16; j++ { for j := i; j < i+16; j++ {
c := buffer[j] & 127 c := buffer[j] & 127
if c >= 32 { if c >= 32 && c < 127 {
fmt.Printf("%c", c) fmt.Printf("%c", c)
} else { } else {
fmt.Printf(".") fmt.Printf(".")