initial commits

master
Charles Iliya Krempeaux 2024-01-23 06:18:36 -08:00
parent ed553874c4
commit f8e5f3fd28
2 changed files with 72 additions and 0 deletions

36
writeheader.go 100644
View File

@ -0,0 +1,36 @@
package raft
import (
"io"
"sourcecode.social/reiver/go-erorr"
)
// writeHeader writes the header of a RAFT file.
//
// It writes:
//
// "RAFT/1\n\n"
//
// Technically, the 2nd "\n" isn't necessary.
// Technically, this is all that is necessary is "RAFT/1\n"
//
// The 2nd "\n" is added to make the resulting RAFT file easier to read (if a person was to look at it and try to read it).
func writeHeader(writer io.Writer) error {
if nil == writer {
return errNilWriter
}
{
var header string = "RAFT/1" + eoleol
err := writeString(writer, header)
if nil != err {
return erorr.Errorf("raft: problem writing first 2 lines of raft file — %q: %w", header, err)
}
}
return nil
}

View File

@ -0,0 +1,36 @@
package raft
import (
"testing"
"strings"
)
func TestWriteHeader(t *testing.T) {
var expected string = "RAFT/1\n\n"
var actual string
{
var buffer strings.Builder
err := writeHeader(&buffer)
if nil != err {
t.Errorf("Did not expect an error but actually got one.")
t.Logf("ERROR: %q", err)
return
}
actual = buffer.String()
}
{
if expected != actual {
t.Errorf("The actual written value is not what was expected.")
t.Logf("EXPECTED: %q", expected)
t.Logf("ACTUAL: %q", actual)
return
}
}
}