testutils

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 25, 2025 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package testutils provides testing utilities and mock implementations for the Zerfoo ML framework.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertContains

func AssertContains(t *testing.T, s, substr, msg string)

AssertContains checks if a string contains a substring.

func AssertEqual

func AssertEqual[T comparable](t *testing.T, expected, actual T, msg string)

AssertEqual checks if two values are equal.

func AssertError

func AssertError(t *testing.T, err error, msg string)

AssertError checks if an error is not nil.

func AssertFalse

func AssertFalse(t *testing.T, condition bool, msg string)

AssertFalse checks if a boolean is false.

func AssertFloat32SliceApproxEqual added in v0.3.0

func AssertFloat32SliceApproxEqual(t *testing.T, expected, actual []float32, tolerance float32, msg string)

AssertFloat32SliceApproxEqual checks if two float32 slices are approximately equal element-wise.

func AssertFloat64SliceApproxEqual added in v0.3.0

func AssertFloat64SliceApproxEqual(t *testing.T, expected, actual []float64, tolerance float64, msg string)

AssertFloat64SliceApproxEqual checks if two float64 slices are approximately equal element-wise.

func AssertFloatEqual

func AssertFloatEqual[T float32 | float64](t *testing.T, expected, actual, tolerance T, msg string)

AssertFloatEqual checks if two float values are approximately equal.

func AssertNil

func AssertNil(t *testing.T, value interface{}, msg string)

AssertNil checks if a value is nil.

func AssertNoError

func AssertNoError(t *testing.T, err error, msg string)

AssertNoError checks if an error is nil.

func AssertNotNil

func AssertNotNil(t *testing.T, value interface{}, msg string)

AssertNotNil checks if a value is not nil.

func AssertPanics

func AssertPanics(t *testing.T, f func(), msg string)

AssertPanics checks if a function panics.

func AssertTrue

func AssertTrue(t *testing.T, condition bool, msg string)

AssertTrue checks if a boolean is true.

func CompareTensorsApprox added in v0.3.0

func CompareTensorsApprox[T tensor.Addable](t *testing.T, actual, expected *tensor.TensorNumeric[T], epsilon T) bool

CompareTensorsApprox checks if two tensors are approximately equal element-wise.

func ElementsMatch

func ElementsMatch(a, b []string) bool

ElementsMatch checks if two string slices contain the same elements, regardless of order.

func Int64SliceEqual added in v0.3.0

func Int64SliceEqual(a, b []int64) bool

Int64SliceEqual checks if two int64 slices are equal.

func IntSliceEqual

func IntSliceEqual(a, b []int) bool

IntSliceEqual checks if two int slices are equal.

func MockClientFactory

func MockClientFactory(_ *grpc.ClientConn) pb.DistributedServiceClient

MockClientFactory is a mock implementation of the DistributedServiceClientFactory function.

func RunTests added in v0.3.0

func RunTests(t *testing.T, tests []TestCase)

RunTests executes a slice of test cases.

Types

type CustomMockDistributedServiceClient

type CustomMockDistributedServiceClient struct {
	// contains filtered or unexported fields
}

CustomMockDistributedServiceClient is a custom mock implementation of the DistributedServiceClient interface.

func (*CustomMockDistributedServiceClient) AllReduce

AllReduce performs an all-reduce operation.

func (*CustomMockDistributedServiceClient) AssertExpectations

func (m *CustomMockDistributedServiceClient) AssertExpectations(t *testing.T)

AssertExpectations asserts that all expected calls were made.

func (*CustomMockDistributedServiceClient) Barrier

Barrier performs a barrier synchronization.

func (*CustomMockDistributedServiceClient) Broadcast

Broadcast performs a broadcast operation.

func (*CustomMockDistributedServiceClient) OnAllReduce

OnAllReduce sets up expectations for the AllReduce method.

func (*CustomMockDistributedServiceClient) ReturnAllReduce

ReturnAllReduce specifies the return value for the AllReduce method.

type CustomMockGrpcServer

type CustomMockGrpcServer struct {
	ServeErr error
	// contains filtered or unexported fields
}

CustomMockGrpcServer is a custom mock implementation of the GrpcServer interface.

func (*CustomMockGrpcServer) AssertExpectations

func (m *CustomMockGrpcServer) AssertExpectations(t *testing.T)

AssertExpectations asserts that all expected calls were made.

func (*CustomMockGrpcServer) GracefulStop

func (m *CustomMockGrpcServer) GracefulStop()

GracefulStop stops the gRPC server gracefully.

func (*CustomMockGrpcServer) OnServe

OnServe sets up expectations for the Serve method.

func (*CustomMockGrpcServer) RegisterService

func (m *CustomMockGrpcServer) RegisterService(_ *grpc.ServiceDesc, _ interface{})

RegisterService registers a service with the mock gRPC server.

func (*CustomMockGrpcServer) Serve

func (m *CustomMockGrpcServer) Serve(_ net.Listener) error

Serve starts serving the mock gRPC server.

func (*CustomMockGrpcServer) Stop

func (m *CustomMockGrpcServer) Stop()

Stop stops the gRPC server.

type CustomMockListener

type CustomMockListener struct {
	AcceptErr error
	CloseErr  error
	AddrVal   net.Addr
	// contains filtered or unexported fields
}

CustomMockListener is a custom mock implementation of the net.Listener interface.

func (*CustomMockListener) Accept

func (m *CustomMockListener) Accept() (net.Conn, error)

Accept waits for and returns the next connection to the listener.

func (*CustomMockListener) Addr

func (m *CustomMockListener) Addr() net.Addr

Addr returns the listener's network address.

func (*CustomMockListener) AssertExpectations

func (m *CustomMockListener) AssertExpectations(t *testing.T)

AssertExpectations asserts that all expected calls were made.

func (*CustomMockListener) Close

func (m *CustomMockListener) Close() error

Close closes the listener.

func (*CustomMockListener) OnAccept

func (m *CustomMockListener) OnAccept(err error) *CustomMockListener

OnAccept sets up expectations for the Accept method.

func (*CustomMockListener) OnAddr

func (m *CustomMockListener) OnAddr(addr net.Addr) *CustomMockListener

OnAddr sets up expectations for the Addr method.

func (*CustomMockListener) OnClose

func (m *CustomMockListener) OnClose(err error) *CustomMockListener

OnClose sets up expectations for the Close method.

type CustomMockLogger

type CustomMockLogger struct {
	// contains filtered or unexported fields
}

CustomMockLogger is a custom mock implementation of the Logger interface.

func (*CustomMockLogger) AssertExpectations

func (m *CustomMockLogger) AssertExpectations(t *testing.T)

AssertExpectations asserts that all expected calls were made.

func (*CustomMockLogger) OnPrintf

func (m *CustomMockLogger) OnPrintf() *CustomMockLogger

OnPrintf sets up expectations for the Printf method.

func (*CustomMockLogger) Printf

func (m *CustomMockLogger) Printf(format string, v ...interface{})

Printf records the arguments and increments the call count for the Printf method.

type CustomMockStrategy

type CustomMockStrategy[T tensor.Numeric] struct {
	BroadcastTensorArgs []struct {
		Tensor   *tensor.TensorNumeric[T]
		RootRank int
	}
	// contains filtered or unexported fields
}

CustomMockStrategy is a custom mock implementation of the InternalStrategy interface.

func (*CustomMockStrategy[T]) AllReduceGradients

func (m *CustomMockStrategy[T]) AllReduceGradients(gradients map[string]*tensor.TensorNumeric[T]) error

AllReduceGradients performs an all-reduce operation on gradients.

func (*CustomMockStrategy[T]) AssertExpectations

func (m *CustomMockStrategy[T]) AssertExpectations(t *testing.T)

AssertExpectations asserts that all expected calls were made.

func (*CustomMockStrategy[T]) AssertNotCalled

func (m *CustomMockStrategy[T]) AssertNotCalled(t *testing.T, methodName string)

AssertNotCalled asserts that a specific method was not called.

func (*CustomMockStrategy[T]) Barrier

func (m *CustomMockStrategy[T]) Barrier() error

Barrier synchronizes all processes.

func (*CustomMockStrategy[T]) BroadcastTensor

func (m *CustomMockStrategy[T]) BroadcastTensor(t *tensor.TensorNumeric[T], rootRank int) error

BroadcastTensor broadcasts a tensor from the root rank to all other processes.

func (*CustomMockStrategy[T]) Init

func (m *CustomMockStrategy[T]) Init(rank, size int, coordinatorAddress string) error

Init records the arguments and increments the call count for the Init method.

func (*CustomMockStrategy[T]) OnAllReduceGradients

func (m *CustomMockStrategy[T]) OnAllReduceGradients(gradients map[string]*tensor.TensorNumeric[T]) *CustomMockStrategy[T]

OnAllReduceGradients sets up expectations for the AllReduceGradients method.

func (*CustomMockStrategy[T]) OnBarrier

func (m *CustomMockStrategy[T]) OnBarrier() *CustomMockStrategy[T]

OnBarrier sets up expectations for the Barrier method.

func (*CustomMockStrategy[T]) OnBroadcastTensor

func (m *CustomMockStrategy[T]) OnBroadcastTensor(t *tensor.TensorNumeric[T], rootRank int) *CustomMockStrategy[T]

OnBroadcastTensor sets up expectations for the BroadcastTensor method.

func (*CustomMockStrategy[T]) OnInit

func (m *CustomMockStrategy[T]) OnInit(rank, size int, coordinatorAddress string) *CustomMockStrategy[T]

OnInit sets up expectations for the Init method.

func (*CustomMockStrategy[T]) OnRank

func (m *CustomMockStrategy[T]) OnRank() *CustomMockStrategy[T]

OnRank sets up expectations for the Rank method.

func (*CustomMockStrategy[T]) OnSize

func (m *CustomMockStrategy[T]) OnSize() *CustomMockStrategy[T]

OnSize sets up expectations for the Size method.

func (*CustomMockStrategy[T]) OnceAllReduceGradients

func (m *CustomMockStrategy[T]) OnceAllReduceGradients() *CustomMockStrategy[T]

OnceAllReduceGradients indicates that the AllReduceGradients method should be called once.

func (*CustomMockStrategy[T]) OnceBroadcastTensor

func (m *CustomMockStrategy[T]) OnceBroadcastTensor() *CustomMockStrategy[T]

OnceBroadcastTensor indicates that the BroadcastTensor method should be called once.

func (*CustomMockStrategy[T]) OnceInit

func (m *CustomMockStrategy[T]) OnceInit() *CustomMockStrategy[T]

OnceInit indicates that the Init method should be called once.

func (*CustomMockStrategy[T]) OnceRank

func (m *CustomMockStrategy[T]) OnceRank() *CustomMockStrategy[T]

OnceRank indicates that the Rank method should be called once.

func (*CustomMockStrategy[T]) OnceSize

func (m *CustomMockStrategy[T]) OnceSize() *CustomMockStrategy[T]

OnceSize indicates that the Size method should be called once.

func (*CustomMockStrategy[T]) Rank

func (m *CustomMockStrategy[T]) Rank() int

Rank returns the rank of the current process.

func (*CustomMockStrategy[T]) ReturnAllReduceGradients

func (m *CustomMockStrategy[T]) ReturnAllReduceGradients(err error) *CustomMockStrategy[T]

ReturnAllReduceGradients specifies the return value for the AllReduceGradients method.

func (*CustomMockStrategy[T]) ReturnBarrier

func (m *CustomMockStrategy[T]) ReturnBarrier(err error) *CustomMockStrategy[T]

ReturnBarrier specifies the return value for the Barrier method.

func (*CustomMockStrategy[T]) ReturnBroadcastTensor

func (m *CustomMockStrategy[T]) ReturnBroadcastTensor(err error) *CustomMockStrategy[T]

ReturnBroadcastTensor specifies the return value for the BroadcastTensor method.

func (*CustomMockStrategy[T]) ReturnInit

func (m *CustomMockStrategy[T]) ReturnInit(err error) *CustomMockStrategy[T]

ReturnInit specifies the return value for the Init method.

func (*CustomMockStrategy[T]) ReturnRank

func (m *CustomMockStrategy[T]) ReturnRank(rank int) *CustomMockStrategy[T]

ReturnRank specifies the return value for the Rank method.

func (*CustomMockStrategy[T]) ReturnSize

func (m *CustomMockStrategy[T]) ReturnSize(size int) *CustomMockStrategy[T]

ReturnSize specifies the return value for the Size method.

func (*CustomMockStrategy[T]) Shutdown

func (m *CustomMockStrategy[T]) Shutdown()

Shutdown performs a graceful shutdown of the distributed training environment.

func (*CustomMockStrategy[T]) Size

func (m *CustomMockStrategy[T]) Size() int

Size returns the total number of processes.

func (*CustomMockStrategy[T]) TwiceBarrier

func (m *CustomMockStrategy[T]) TwiceBarrier() *CustomMockStrategy[T]

TwiceBarrier indicates that the Barrier method should be called twice.

type FailingInitializer added in v0.3.0

type FailingInitializer[T tensor.Numeric] struct{}

FailingInitializer is a test initializer that always returns an error.

func (*FailingInitializer[T]) Initialize added in v0.3.0

func (f *FailingInitializer[T]) Initialize(_, _ int) ([]T, error)

Initialize always returns an error for testing error paths.

type MockEngine

type MockEngine[T tensor.Numeric] struct {
	compute.Engine[T]
	Err error
}

MockEngine is a mock implementation of the compute.Engine interface.

func NewMockEngine

func NewMockEngine[T tensor.Numeric]() *MockEngine[T]

NewMockEngine creates a new mock engine for testing.

func NewMockEngineWithError

func NewMockEngineWithError[T tensor.Numeric](err error) *MockEngine[T]

NewMockEngineWithError creates a new mock engine that returns the specified error.

func (*MockEngine[T]) Add

func (e *MockEngine[T]) Add(_ context.Context, _, _ *tensor.TensorNumeric[T], _ ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Add performs element-wise addition of two tensors.

func (*MockEngine[T]) Allocator

func (e *MockEngine[T]) Allocator() device.Allocator

Allocator returns the memory allocator for the engine.

func (*MockEngine[T]) Close

func (e *MockEngine[T]) Close() error

Close closes the engine and releases resources.

func (*MockEngine[T]) Context

func (e *MockEngine[T]) Context() context.Context

Context returns the context associated with the engine.

func (*MockEngine[T]) Copy

func (e *MockEngine[T]) Copy(_ context.Context, _, _ *tensor.TensorNumeric[T]) error

Copy copies data from source tensor to destination tensor.

func (*MockEngine[T]) Device

func (e *MockEngine[T]) Device() device.Device

Device returns the device associated with the engine.

func (*MockEngine[T]) Div

func (e *MockEngine[T]) Div(_ context.Context, _, _ *tensor.TensorNumeric[T], _ ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Div performs element-wise division of two tensors.

func (*MockEngine[T]) Exp

Exp computes the exponential of tensor elements.

func (*MockEngine[T]) Log

Log computes the natural logarithm of tensor elements.

func (*MockEngine[T]) MatMul

func (e *MockEngine[T]) MatMul(_ context.Context, _, _ *tensor.TensorNumeric[T], _ ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

MatMul performs matrix multiplication of two tensors.

func (*MockEngine[T]) Mul

func (e *MockEngine[T]) Mul(_ context.Context, _, _ *tensor.TensorNumeric[T], _ ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Mul performs element-wise multiplication of two tensors.

func (*MockEngine[T]) Name

func (e *MockEngine[T]) Name() string

Name returns the name of the engine.

func (*MockEngine[T]) Pow

func (e *MockEngine[T]) Pow(_ context.Context, _, _ *tensor.TensorNumeric[T], _ ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Pow computes the power of base tensor raised to exponent tensor.

func (*MockEngine[T]) String

func (e *MockEngine[T]) String() string

func (*MockEngine[T]) Sub

func (e *MockEngine[T]) Sub(_ context.Context, _, _ *tensor.TensorNumeric[T], _ ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Sub performs element-wise subtraction of two tensors.

func (*MockEngine[T]) Sum

func (e *MockEngine[T]) Sum(_ context.Context, _ *tensor.TensorNumeric[T], _ int, _ bool, _ ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Sum computes the sum of tensor elements along the specified axis.

func (*MockEngine[T]) Transpose

func (e *MockEngine[T]) Transpose(_ context.Context, _ *tensor.TensorNumeric[T], _ []int, _ ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Transpose transposes a tensor along the specified axes.

func (*MockEngine[T]) UnaryOp

func (e *MockEngine[T]) UnaryOp(_ context.Context, _ *tensor.TensorNumeric[T], _ func(T) T, _ ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

UnaryOp performs a unary operation on a tensor.

func (*MockEngine[T]) Wait

func (e *MockEngine[T]) Wait() error

Wait waits for all pending operations to complete.

func (*MockEngine[T]) WithAllocator

func (e *MockEngine[T]) WithAllocator(_ device.Allocator) compute.Engine[T]

WithAllocator returns a new engine with the specified allocator.

func (*MockEngine[T]) WithContext

func (e *MockEngine[T]) WithContext(_ context.Context) compute.Engine[T]

WithContext returns a new engine with the specified context.

func (*MockEngine[T]) WithDevice

func (e *MockEngine[T]) WithDevice(_ device.Device) compute.Engine[T]

WithDevice returns a new engine with the specified device.

func (*MockEngine[T]) WithError

func (e *MockEngine[T]) WithError(err error) *MockEngine[T]

WithError returns a mock engine that will return the specified error.

func (*MockEngine[T]) WithName

func (e *MockEngine[T]) WithName(_ string) compute.Engine[T]

WithName returns a new engine with the specified name.

func (*MockEngine[T]) Zero

func (e *MockEngine[T]) Zero(_ context.Context, _ *tensor.TensorNumeric[T]) error

Zero sets all elements of the tensor to zero.

type TestCase added in v0.3.0

type TestCase struct {
	Name string
	Func func(t *testing.T)
}

TestCase represents a single test case with a name and a function to execute.

type TestInitializer added in v0.3.0

type TestInitializer[T tensor.Numeric] struct {
	Value T
}

TestInitializer is a test initializer that sets all values to a specific value.

func (*TestInitializer[T]) Initialize added in v0.3.0

func (t *TestInitializer[T]) Initialize(inputSize, outputSize int) ([]T, error)

Initialize sets all values to the specified value.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL