github.com/haskelladdict/stringo@v0.0.0-20131201010643-24e675025fd4/lrs/lrs_test.go (about)

     1  // lrs_test implements a few small tests and benchmarks for the
     2  // lowest repeated substring code
     3  
     4  package main
     5  
     6  
     7  import (
     8    "io/ioutil"
     9    "log"
    10    "testing"
    11  )
    12  
    13  
    14  // test and benchmark 1
    15  var input_string_1 string
    16  func init() {
    17    input_bytes_1, err := ioutil.ReadFile("test_files/moby_dick.txt")
    18    if err != nil {
    19      log.Fatal("Failed to open and read file.")
    20    }
    21  
    22    input_string_1 = string(input_bytes_1)
    23  }
    24  
    25  
    26  
    27  // TestLrs_1 tests the proper determination of the longest repeated
    28  // substring
    29  func TestLrs_1(t *testing.T) {
    30  
    31    target_string := ",--  Such a funny, sporty, gamy, jesty, joky, hoky-poky lad, is the Ocean, oh!  Th"
    32    target_length := 82
    33  
    34    longest_string := lrs_h(input_string_1)
    35  
    36    if longest_string != target_string || len(longest_string) != target_length {
    37      t.Errorf("Failed to identify correct longest repeated string")
    38    }
    39  }
    40  
    41  
    42  // BenchmarkLrs_1 benchmarks the computation of the lrs
    43  func BenchmarkLrs_1(t *testing.B) {
    44    lrs_h(input_string_1)
    45  }
    46  
    47  
    48  
    49  
    50  // test 2
    51  var input_string_2 string
    52  func init() {
    53    input_bytes_2, err := ioutil.ReadFile("test_files/short_test.txt")
    54    if err != nil {
    55      log.Fatal("Failed to open and read file.")
    56    }
    57  
    58    input_string_2 = string(input_bytes_2)
    59  }
    60  
    61  
    62  
    63  // TestLrs_2 tests the proper determination of the longest repeated
    64  // substring
    65  func TestLrs_2(t *testing.T) {
    66  
    67    target_string := " have never had a "
    68    target_length := 18
    69  
    70    longest_string := lrs_h(input_string_2)
    71  
    72    if longest_string != target_string || len(longest_string) != target_length {
    73      t.Errorf("Failed to identify correct longest repeated string")
    74    }
    75  }
    76  
    77  
    78  
    79  // BenchmarkLrs_2 benchmarks the computation of the lrs
    80  func BenchmarkLrs_2(t *testing.B) {
    81    lrs_h(input_string_2)
    82  }
    83  
    84