github.com/KEINOS/go-countline@v1.1.0/cl/spec/spec_test.go (about)

     1  package spec
     2  
     3  import (
     4  	"bufio"
     5  	"fmt"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/KEINOS/go-countline/cl"
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestGetStrDummyLines(t *testing.T) {
    15  	t.Parallel()
    16  
    17  	const sizeLine = bufio.MaxScanTokenSize * 2
    18  
    19  	for _, numLine := range []int64{
    20  		1,
    21  		2,
    22  	} {
    23  		result := GetStrDummyLines(sizeLine, numLine)
    24  		require.NotEmpty(t, result, "the result should not be empty")
    25  
    26  		{
    27  			expectLen := (65536 * 2) * numLine // bufio.MaxScanTokenSize * <num of lines> + <num of line breaks>
    28  			actualLen := int64(len(result))
    29  
    30  			require.Equal(t, expectLen, actualLen, "byte length miss match. numLine: %v", numLine)
    31  		}
    32  		{
    33  			expectCount := numLine
    34  			actualCount := int64(strings.Count(result, "\n"))
    35  
    36  			require.Equal(t, expectCount, actualCount, "it should have %v line breaks", numLine)
    37  		}
    38  		{
    39  			expectHead := "a"
    40  			actualHead := string(result[0])
    41  
    42  			require.Equal(t, expectHead, actualHead, "the first character should be 'a'")
    43  		}
    44  		{
    45  			expectEnd := byte('\n')
    46  			actualEnd := result[len(result)-1]
    47  
    48  			require.Equal(t, expectEnd, actualEnd, "the last character should be a line break")
    49  		}
    50  	}
    51  }
    52  
    53  func TestRunSpecTest(t *testing.T) {
    54  	t.Parallel()
    55  
    56  	require.NotPanics(t, func() {
    57  		RunSpecTest(t, "CountLines", cl.CountLines)
    58  	})
    59  }
    60  
    61  func Test_genOneLine(t *testing.T) {
    62  	t.Parallel()
    63  
    64  	{
    65  		dataLine := genOneLine(0)
    66  		require.Empty(t, dataLine, "line size 0 should be empty")
    67  	}
    68  
    69  	for index, test := range []struct {
    70  		name        string
    71  		expectFirst byte
    72  		expectLast  byte
    73  		requestSize int64
    74  	}{
    75  		{
    76  			name:        "line size of 1 should have length of 1 and contains only \\n",
    77  			requestSize: 1,
    78  			expectFirst: byte(0x0a),
    79  			expectLast:  byte(0x0a),
    80  		},
    81  		{
    82  			name:        "line size of 2 should have length of 2 and contains only 'a\\n'",
    83  			requestSize: 2,
    84  			expectFirst: byte('a'),
    85  			expectLast:  byte(0x0a),
    86  		},
    87  		{
    88  			name:        "line size of 100 should have length of 100 and contains only 'a' and the last char as '\\n'",
    89  			requestSize: 100,
    90  			expectFirst: byte('a'),
    91  			expectLast:  byte(0x0a),
    92  		},
    93  	} {
    94  		dataLine := genOneLine(test.requestSize)
    95  		t.Logf("Generated data: %#v", dataLine)
    96  
    97  		reason := fmt.Sprintf("test %d: %s", index+1, test.name)
    98  
    99  		require.Equal(t, int64(len(dataLine)), test.requestSize, reason)
   100  
   101  		assert.Equal(t, dataLine[0], test.expectFirst, "first char did not match.\n%s", reason)
   102  
   103  		for i := 0; i < len(dataLine)-1; i++ {
   104  			assert.Equal(t, dataLine[i], byte('a'), "index %d should be 'a'\n%s", i, reason)
   105  		}
   106  
   107  		assert.Equal(t, dataLine[len(dataLine)-1], test.expectLast, "last char did not match. %s", reason)
   108  	}
   109  }