github.com/vpayno/adventofcode-2022-golang-workspace@v0.0.0-20230605190011-dbafed5593de/internal/day04/app_test.go (about)

     1  package day04
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/vpayno/adventofcode-2022-golang-workspace/internal/aocshared"
     9  )
    10  
    11  /*
    12    Stdout testing code borrowed from Jon Calhoun's FizzBuzz example.
    13    https://courses.calhoun.io/lessons/les_algo_m01_08
    14    https://github.com/joncalhoun/algorithmswithgo.com/blob/master/module01/fizz_buzz_test.go
    15  */
    16  
    17  // This is the main test function. This is the gatekeeper of all the tests in the appwc package.
    18  func TestMain(m *testing.M) {
    19  	exitCode := m.Run()
    20  	os.Exit(exitCode)
    21  }
    22  
    23  func TestRun(t *testing.T) {
    24  	conf := Setup("day04")
    25  
    26  	err := Run(conf)
    27  	var cause string
    28  	if err != nil {
    29  		cause = err.Error()
    30  	}
    31  	assert.Nil(t, err, cause)
    32  }
    33  
    34  func TestRun_missingFile(t *testing.T) {
    35  	// this tests fails because it can't find the file
    36  	conf := Setup("day00")
    37  
    38  	err := Run(conf)
    39  	assert.NotNil(t, err, "Run() didn't fail with a can't find file error")
    40  }
    41  
    42  func TestRun_unevenPairs(t *testing.T) {
    43  	// this tests fails because the not all the sack groups have 3 elves
    44  	conf := Setup("day04")
    45  
    46  	// Give it bad data.
    47  	conf.inputFileName = "data/day04/day04-input-bad_pair_size.txt"
    48  
    49  	err := Run(conf)
    50  	assert.NotNil(t, err, "Run() didn't fail with a pair size error")
    51  }
    52  
    53  func TestRun_notANumberStart(t *testing.T) {
    54  	// this tests fails because there are no shared items
    55  	conf := Setup("day04")
    56  
    57  	// Give it bad data.
    58  	conf.inputFileName = "data/day04/day04-input-not_a_number1.txt"
    59  
    60  	err := Run(conf)
    61  	assert.NotNil(t, err, "Run() didn't fail with a string to int conversion error")
    62  }
    63  
    64  func TestRun_notANumberEnd(t *testing.T) {
    65  	// this tests fails because there are no shared items
    66  	conf := Setup("day04")
    67  
    68  	// Give it bad data.
    69  	conf.inputFileName = "data/day04/day04-input-not_a_number2.txt"
    70  
    71  	err := Run(conf)
    72  	assert.NotNil(t, err, "Run() didn't fail with a string to int conversion error")
    73  }
    74  
    75  func TestRun_incompleteRange(t *testing.T) {
    76  	// this tests fails because there are no shared items
    77  	conf := Setup("day04")
    78  
    79  	// Give it bad data.
    80  	conf.inputFileName = "data/day04/day04-input-incomplete_range.txt"
    81  
    82  	err := Run(conf)
    83  	assert.NotNil(t, err, "Run() didn't fail with an incomplete range error")
    84  }
    85  
    86  func TestRun_badStartRange(t *testing.T) {
    87  	// this tests fails because there are no shared items
    88  	conf := Setup("day04")
    89  
    90  	// Give it bad data.
    91  	conf.inputFileName = "data/day04/day04-input-bad_range_start.txt"
    92  
    93  	err := Run(conf)
    94  	assert.NotNil(t, err, "Run() didn't fail with a bad range start value error")
    95  }
    96  
    97  func TestRun_badEndRange(t *testing.T) {
    98  	// this tests fails because there are no shared items
    99  	conf := Setup("day04")
   100  
   101  	// Give it bad data.
   102  	conf.inputFileName = "data/day04/day04-input-bad_range_end.txt"
   103  
   104  	err := Run(conf)
   105  	assert.NotNil(t, err, "Run() didn't fail with a bad range start value error")
   106  }
   107  
   108  func TestLoadData(t *testing.T) {
   109  	wantLen := 1_000
   110  
   111  	fileName := "data/day04/day04-input.txt"
   112  
   113  	file, err := aocshared.GetFile(fileName)
   114  	assert.Nil(t, err, err)
   115  
   116  	scanner := aocshared.GetScanner(file)
   117  
   118  	got, err := loadData(scanner)
   119  	assert.Nil(t, err, err)
   120  	gotLen := len(got)
   121  
   122  	assert.Equal(t, wantLen, gotLen, "read data is the wrong size")
   123  }
   124  
   125  func TestLoadData_blankLine(t *testing.T) {
   126  	wantLen := 4
   127  
   128  	fileName := "data/day04/day04-input-blank_line.txt"
   129  
   130  	file, err := aocshared.GetFile(fileName)
   131  	assert.Nil(t, err, err)
   132  
   133  	scanner := aocshared.GetScanner(file)
   134  
   135  	got, err := loadData(scanner)
   136  	assert.Nil(t, err, err)
   137  	gotLen := len(got)
   138  
   139  	assert.Equal(t, wantLen, gotLen, "read data is the wrong size")
   140  }
   141  
   142  func TestLoadData_badPairSize(t *testing.T) {
   143  	fileName := "data/day04/day04-input-bad_pair_size.txt"
   144  
   145  	file, err := aocshared.GetFile(fileName)
   146  	assert.Nil(t, err, err)
   147  
   148  	scanner := aocshared.GetScanner(file)
   149  
   150  	_, err = loadData(scanner)
   151  	assert.NotNil(t, err, err)
   152  }
   153  
   154  func TestLoadData_notANumberStart(t *testing.T) {
   155  	fileName := "data/day04/day04-input-not_a_number1.txt"
   156  
   157  	file, err := aocshared.GetFile(fileName)
   158  	assert.Nil(t, err, err)
   159  
   160  	scanner := aocshared.GetScanner(file)
   161  
   162  	_, err = loadData(scanner)
   163  	assert.NotNil(t, err, err)
   164  }
   165  
   166  func TestLoadData_notANumberEnd(t *testing.T) {
   167  	fileName := "data/day04/day04-input-not_a_number2.txt"
   168  
   169  	file, err := aocshared.GetFile(fileName)
   170  	assert.Nil(t, err, err)
   171  
   172  	scanner := aocshared.GetScanner(file)
   173  
   174  	_, err = loadData(scanner)
   175  	assert.NotNil(t, err, err)
   176  }
   177  
   178  func TestLoadData_incompleteRange(t *testing.T) {
   179  	fileName := "data/day04/day04-input-incomplete_range.txt"
   180  
   181  	file, err := aocshared.GetFile(fileName)
   182  	assert.Nil(t, err, err)
   183  
   184  	scanner := aocshared.GetScanner(file)
   185  
   186  	_, err = loadData(scanner)
   187  	assert.NotNil(t, err, err)
   188  }
   189  
   190  func TestLoadData_badRangeStart(t *testing.T) {
   191  	fileName := "data/day04/day04-input-bad_range_start.txt"
   192  
   193  	file, err := aocshared.GetFile(fileName)
   194  	assert.Nil(t, err, err)
   195  
   196  	scanner := aocshared.GetScanner(file)
   197  
   198  	_, err = loadData(scanner)
   199  	assert.NotNil(t, err, err)
   200  }
   201  
   202  func TestLoadData_badRangeEnd(t *testing.T) {
   203  	fileName := "data/day04/day04-input-bad_range_end.txt"
   204  
   205  	file, err := aocshared.GetFile(fileName)
   206  	assert.Nil(t, err, err)
   207  
   208  	scanner := aocshared.GetScanner(file)
   209  
   210  	_, err = loadData(scanner)
   211  	assert.NotNil(t, err, err)
   212  }
   213  
   214  func TestGetFullyContainedCount(t *testing.T) {
   215  	groups := pairs{}
   216  
   217  	p := pair{}
   218  
   219  	data := []string{
   220  		"2-4,6-8",
   221  		"2-3,4-5",
   222  		"5-7,7-9",
   223  		"2-8,3-7",
   224  		"6-6,4-6",
   225  		"2-6,4-8",
   226  	}
   227  
   228  	for _, line := range data {
   229  		err := p.addPair(line)
   230  		assert.Nil(t, err, err)
   231  		groups = append(groups, p)
   232  	}
   233  
   234  	want := 2
   235  
   236  	got := getFullyContainedCount(groups)
   237  
   238  	assert.Equal(t, want, got)
   239  }
   240  
   241  func TestGetPartiallyContainedCount(t *testing.T) {
   242  	groups := pairs{}
   243  
   244  	p := pair{}
   245  
   246  	data := []string{
   247  		"2-4,6-8",
   248  		"2-3,4-5",
   249  		"5-7,7-9",
   250  		"2-8,3-7",
   251  		"6-6,4-6",
   252  		"2-6,4-8",
   253  	}
   254  
   255  	for _, line := range data {
   256  		err := p.addPair(line)
   257  		assert.Nil(t, err, err)
   258  		groups = append(groups, p)
   259  	}
   260  
   261  	want := 4
   262  
   263  	got := getPartiallyContainedCount(groups)
   264  
   265  	assert.Equal(t, want, got)
   266  }