common

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidBase occurs when the base is zero.
	//
	// Format:
	//   "base must not be zero"
	ErrInvalidBase error

	// ErrNilReceiver occurs when the receiver is nil.
	//
	// Format:
	//   "receiver must not be nil"
	ErrNilReceiver error

	// NoTestInstance occurs when a nil testing instance was provided.
	//
	// Format:
	//   "no testing instance was provided"
	NoTestInstance error
)
View Source
var FAIL failT

FAIL is the namespace for making errors.

Functions

func NewErrTestFailed

func NewErrTestFailed(want string, got string) error

NewErrTestFailed creates a new error that represents a test failure. This should only be used when there are no FAIL.Wrong* functions that suit your use case.

Parameters:

  • want: The expected value.
  • got: The actual value encountered.

Returns:

  • error: The new error. Never returns nil.

Format:

"want <want>, got <got>"

where:

  • <want> is the expected value.
  • <got> is the actual value.

func OrQuoteElse

func OrQuoteElse(str string, def string) string

OrQuoteElse returns the quoted string if the string is not empty. Otherwise, it returns the default string.

Parameters:

  • str: The string to quote if it is not empty.
  • def: The default string to return if str is empty.

Returns:

  • string: The quoted string or the default string.

func UintPow

func UintPow(base, exp uint) (uint, error)

UintPow calculates base to the power exp, where base and exp are unsigned integers. It does this efficiently by using exponentiation by squaring.

Parameters:

  • base: The base of the exponentiation.
  • exp: The exponent.

Returns:

  • uint: The result of the exponentiation.
  • error: An error of type ErrInvalidBase if the base is zero.

Example:

UintPow(2, 8) // Returns 256

func UintPowSlice

func UintPowSlice(base uint, max_exp uint) ([]uint, error)

UintPowSlice calculates the slice of all powers of the given base up to the maximum exponent.

Parameters:

  • base: The base of the exponentiation.
  • max_exp: The maximum exponent.

Returns:

  • []uint: A slice of all powers of base from 0 to max_exp.
  • error: An error of type ErrInvalidBase if the base is zero.

Example:

UintPowSlice(2, 8) // Returns [1, 2, 4, 8, 16, 32, 64, 128, 256]

Types

type ErrTestFailed

type ErrTestFailed struct {
	// Want is the expected value.
	Want string

	// Got is the actual value encountered.
	Got string
}

ErrTestFailed is an error that represents a test failure.

func (ErrTestFailed) Error

func (e ErrTestFailed) Error() string

Error implements the error interface.

Jump to

Keyboard shortcuts

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