Files
zeekay abf99d05fc papers: reorganize into per-paper dirs, add Antje Worring as co-author
Restructured from flat .tex/.pdf files into per-paper subdirectories.
Added Antje Worring as co-author on all papers.
Synced PDFs to pdfs/ for download site.
2026-04-18 21:50:07 -07:00

72 lines
2.1 KiB
TeX

\section{Layer 1: Unit Tests}
\subsection{What to Test}
Unit tests verify individual functions in isolation. For a blockchain node:
\begin{itemize}[nosep]
\item Serialization/deserialization of messages, blocks, transactions.
\item Cryptographic operations (signature, hash, verification).
\item State transition logic (apply block to state).
\item Data structure operations (merkle tree insert/proof/verify).
\item Validator set management (add, remove, weight calculation).
\end{itemize}
\subsection{Go Test Patterns}
\begin{lstlisting}[language=Go,caption={Table-driven unit test}]
func TestBlockValidation(t *testing.T) {
tests := []struct {
name string
block Block
wantErr error
}{
{
name: "valid block",
block: validBlock(),
wantErr: nil,
},
{
name: "empty parent hash",
block: blockWithEmptyParent(),
wantErr: ErrInvalidParentHash,
},
{
name: "timestamp in future",
block: blockWithFutureTimestamp(),
wantErr: ErrFutureBlock,
},
{
name: "insufficient signatures",
block: blockWithTooFewSigs(),
wantErr: ErrInsufficientQuorum,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateBlock(tt.block)
if !errors.Is(err, tt.wantErr) {
t.Errorf("ValidateBlock() = %v, want %v",
err, tt.wantErr)
}
})
}
}
\end{lstlisting}
\subsection{Coverage Requirements}
We require 80\% line coverage for the node codebase. More importantly, we require 100\%
coverage of error paths: every \texttt{if err != nil} branch must be exercised by a test
that triggers that error.
\begin{lstlisting}[language=bash,caption={Running Go tests with coverage}]
go test -v -race -coverprofile=coverage.out ./...
go tool cover -func=coverage.out | grep total
# total: (statements) 83.2%
\end{lstlisting}
%% ============================================================