From 4fa573260c12ac1a9a1b42d4d2910213c10c4c74 Mon Sep 17 00:00:00 2001 From: Terence Boldt Date: Thu, 14 Mar 2024 07:32:03 -0500 Subject: [PATCH] Move and add tests --- prodos/bitmap_test.go | 25 ++++++++++++- prodos/directory.go | 4 +- prodos/directory_test.go | 79 ++++++++++++++++++++++++++++++++++++++++ prodos/file_test.go | 23 ------------ 4 files changed, 105 insertions(+), 26 deletions(-) create mode 100644 prodos/directory_test.go diff --git a/prodos/bitmap_test.go b/prodos/bitmap_test.go index d464e7b..3161da0 100644 --- a/prodos/bitmap_test.go +++ b/prodos/bitmap_test.go @@ -2,7 +2,7 @@ // Use of this source code is governed by an MIT // license that can be found in the LICENSE file. -// This file provides tests for access to volum bitmap on +// This file provides tests for access to volume bitmap on // a ProDOS drive image package prodos @@ -89,3 +89,26 @@ func TestMarkBlockInVolumeBitmap(t *testing.T) { }) } } + +func TestUpdateVolumeBitmap(t *testing.T) { + blockList := []uint16{10, 11, 12, 100, 120} + + virtualDisk := NewMemoryFile(0x2000000) + CreateVolume(virtualDisk, "VIRTUAL.DISK", 0xFFFE) + updateVolumeBitmap(virtualDisk, blockList) + + for _, tt := range blockList { + testname := fmt.Sprintf("%d", tt) + t.Run(testname, func(t *testing.T) { + + volumeBitmap, err := ReadVolumeBitmap(virtualDisk) + if err != nil { + t.Error("got error, want nil") + } + free := checkFreeBlockInVolumeBitmap(volumeBitmap, tt) + if free { + t.Errorf("got true, want false") + } + }) + } +} diff --git a/prodos/directory.go b/prodos/directory.go index ea574b6..eaaf1c9 100644 --- a/prodos/directory.go +++ b/prodos/directory.go @@ -3,7 +3,7 @@ // license that can be found in the LICENSE file. // This file provides access to read, write, delete -// fand parse directories on a ProDOS drive image +// and parse directories on a ProDOS drive image package prodos @@ -117,7 +117,7 @@ func ReadDirectory(reader io.ReaderAt, path string) (VolumeHeader, DirectoryHead // on a ProDOS image func CreateDirectory(readerWriter ReaderWriterAt, path string) error { if len(path) == 0 { - return errors.New("cannot create directory with path") + return errors.New("cannot create directory without path") } // add volume name if not full path diff --git a/prodos/directory_test.go b/prodos/directory_test.go new file mode 100644 index 0000000..bf3a00b --- /dev/null +++ b/prodos/directory_test.go @@ -0,0 +1,79 @@ +// Copyright Terence J. Boldt (c)2024 +// Use of this source code is governed by an MIT +// license that can be found in the LICENSE file. + +// This file provides tests for directories on +// a ProDOS drive image + +package prodos + +import ( + "testing" +) + +func TestCreateDirectoryWithoutPathFails(t *testing.T) { + t.Run("TestCreateDirectoryWithoutPathFails", func(t *testing.T) { + file := NewMemoryFile(0x2000000) + + CreateVolume(file, "test.volume", 1024) + + err := CreateDirectory(file, "") + + if err == nil { + t.Errorf("got nil, want non-nil") + } + }) +} + +func TestCreateDuplicateDirectoryFails(t *testing.T) { + t.Run("TestCreateDuplicateDirectoryFails", func(t *testing.T) { + file := NewMemoryFile(0x2000000) + + CreateVolume(file, "test.volume", 1024) + + err := CreateDirectory(file, "duplicate") + if err != nil { + t.Errorf("failed to create directory: %s", err) + } + + err = CreateDirectory(file, "duplicate") + + if err == nil { + t.Error("got nil, want non-nil") + } + }) +} + +func TestCreateAndReadDirectory(t *testing.T) { + var tests = []struct { + testName string + createPath string + readPath string + expectedCount int + }{ + {"checkRoot", "", "/test", 0}, + {"checkCreateInRoot", "one", "/test", 1}, + {"checkRootCreateInSub", "/test/one/two", "/test", 1}, + {"checkSub", "", "/test/one", 1}, + } + + file := NewMemoryFile(0x2000000) + CreateVolume(file, "test", 1024) + + for _, tt := range tests { + t.Run(tt.testName, func(t *testing.T) { + if len(tt.createPath) > 0 { + CreateDirectory(file, tt.createPath) + } + _, _, fileEntries, err := ReadDirectory(file, tt.readPath) + + if err != nil { + t.Errorf("got error %s", err) + } + got := len(fileEntries) + if got != int(tt.expectedCount) { + t.Errorf("got %d, want %d", got, tt.expectedCount) + } + }) + } +} diff --git a/prodos/file_test.go b/prodos/file_test.go index 37ac6ab..c17d27c 100644 --- a/prodos/file_test.go +++ b/prodos/file_test.go @@ -39,26 +39,3 @@ func TestCreateBlocklist(t *testing.T) { }) } } - -func TestUpdateVolumeBitmap(t *testing.T) { - blockList := []uint16{10, 11, 12, 100, 120} - - virtualDisk := NewMemoryFile(0x2000000) - CreateVolume(virtualDisk, "VIRTUAL.DISK", 0xFFFE) - updateVolumeBitmap(virtualDisk, blockList) - - for _, tt := range blockList { - testname := fmt.Sprintf("%d", tt) - t.Run(testname, func(t *testing.T) { - - volumeBitmap, err := ReadVolumeBitmap(virtualDisk) - if err != nil { - t.Error("got error, want nil") - } - free := checkFreeBlockInVolumeBitmap(volumeBitmap, tt) - if free { - t.Errorf("got true, want false") - } - }) - } -}