_alt

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2022 License: MIT Imports: 7 Imported by: 0

README

Alternate implementations of the CountLines function

This directory contains alternate implementations of the CountLines function.

New implementations must be placed in this directory first and Pull-Requested. After benchmarking on code-review, they may be swapped into the main function.

Files to change

You need to create and edit the following 3 files:

  1. Create a new file for your new implementation.
  2. Add your function to the TestCountLines_specs function in alt_test.go.
  3. Add your function to the targetFuncions variable in ../benchmarks_test.go.
Create a file
  • File name: altN.go where N is the next available number. e.g. alt4.go
// Replace the `N` in `CountLinesAltN` with the latest number of
// implementations. e.g. `CountLinesAlt4`
func CountLinesAltN(r io.Reader) (int, error) {
    // Your implementation here
}
Add the function to the test list
  • File name: alt_test.go
func TestCountLines_specs(t *testing.T) {
    for _, targetFunc := range []struct {
        name string
        fn   func(io.Reader) (int, error)
    }{
        // Add the alternate implementations here.
        {"CountLinesAlt1", CountLinesAlt1},
        {"CountLinesAlt2", CountLinesAlt2},
        {"CountLinesAlt3", CountLinesAlt3},
+       {"CountLinesAlt4", CountLinesAlt4},
    } {
        t.Run(targetFunc.name, func(t *testing.T) {
            spec.RunSpecTest(t, targetFunc.name, targetFunc.fn)
        })
    }
}
Add the function to the benchmark list
  • File name: ../benchmarks_test.go
var targetFuncions = map[string]struct {
    fn func(io.Reader) (int, error)
}{
    // Current implementation
    "CountLinesCurr": {CountLines},
    // Alternate implementation. See alt_test.go.
    "CountLinesAlt1": {_alt.CountLinesAlt1},
    "CountLinesAlt2": {_alt.CountLinesAlt2},
    "CountLinesAlt3": {_alt.CountLinesAlt3},
+   "CountLinesAlt4": {_alt.CountLinesAlt4},
}

Regulations

You need the following to be covered in your implementation:

Use the make command for convenience.

$ # Runs unit tests, lints, and cove coverage.
$ make test
...

$ make benchmark
...

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CountLinesAlt1

func CountLinesAlt1(inputReader io.Reader) (int, error)

CountLinesAlt1 uses a Transformer to count the number of lines in a file.

func CountLinesAlt2

func CountLinesAlt2(inputReader io.Reader) (int, error)

CountLinesAlt2 uses bufio.Reader and goroutines to count the number of lines.

func CountLinesAlt3

func CountLinesAlt3(inputReader io.Reader) (int, error)

CountLinesAlt3 is the 3rd attempt to count the number of lines in a file using bufio.Reader without goroutines.

func CountLinesAlt4

func CountLinesAlt4(inputReader io.Reader) (int, error)

CountLinesAlt4 uses atomic and goroutines to count the number of lines.

func CountLinesAlt5 added in v1.1.0

func CountLinesAlt5(inputReader io.Reader) (int, error)

CountLinesAlt5 uses bufio.Scanner to count the number of lines.

Types

type LineCounterAlt1

type LineCounterAlt1 struct {
	LenRead      int
	Count        int
	HasFragments bool
}

func (*LineCounterAlt1) Reset

func (lc *LineCounterAlt1) Reset()

func (*LineCounterAlt1) Transform

func (lc *LineCounterAlt1) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error)

Transform is the implementation of the Transformer interface.

Jump to

Keyboard shortcuts

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