github.com/balzaczyy/golucene@v0.0.0-20151210033525-d0be9ee89713/core/util/automaton/minimize_test.go (about)

     1  package automaton
     2  
     3  import (
     4  	. "github.com/balzaczyy/golucene/test_framework/util"
     5  	. "github.com/balzaczyy/gounit"
     6  	"testing"
     7  )
     8  
     9  func TestMinimizeCase1(t *testing.T) {
    10  	// for i := 0; i < 20; i++ {
    11  	s1 := string([]rune{46, 93, 42, 9794, 64126})
    12  	s2 := string([]rune{46, 453, 46, 91, 64417, 65, 65533, 65533, 93, 46, 42, 93, 124, 124})
    13  	a1 := complement(NewRegExpWithFlag(s1, NONE).ToAutomaton())
    14  	a2 := complement(NewRegExpWithFlag(s2, NONE).ToAutomaton())
    15  	a := minus(a1, a2)
    16  	b := minimize(a)
    17  	assert(sameLanguage(a, b))
    18  	// }
    19  }
    20  
    21  func TestMinimizeCase2(t *testing.T) {
    22  	// for i := 0; i < 20; i++ {
    23  	s1 := ")]"
    24  	s2 := "]"
    25  	a1 := complement(NewRegExpWithFlag(s1, NONE).ToAutomaton())
    26  	a2 := complement(NewRegExpWithFlag(s2, NONE).ToAutomaton())
    27  	a := minus(a1, a2)
    28  	b := minimize(a)
    29  	assert(sameLanguage(a, b))
    30  	// }
    31  }
    32  
    33  func TestMinimizeCase3(t *testing.T) {
    34  	s := "*.?-"
    35  	r := NewRegExpWithFlag(s, NONE)
    36  	a := r.ToAutomaton()
    37  	b := minimize(a)
    38  	assert(sameLanguage(a, b))
    39  }
    40  
    41  func TestRemoveDeadStatesSimple(t *testing.T) {
    42  	a := newEmptyAutomaton()
    43  	a.createState()
    44  	assert(a.numStates() == 1)
    45  	a = removeDeadStates(a)
    46  	assert(a.numStates() == 0)
    47  }
    48  
    49  // util/automaton/TestMinimize.java
    50  // This test builds some randomish NFA/DFA and minimizes them.
    51  
    52  // The minimal and non-minimal are compared to ensure they are the same.
    53  func TestMinimize(t *testing.T) {
    54  	num := AtLeast(200)
    55  	for i := 0; i < num; i++ {
    56  		a := randomAutomaton(Random())
    57  		la := determinize(removeDeadStates(a))
    58  		lb := minimize(a)
    59  		It(t).Should("have same language for %v and %v from %v", la, lb, a).
    60  			Verify(sameLanguage(la, lb))
    61  	}
    62  }
    63  
    64  /*
    65  Compare minimized against minimized with a slower, simple impl. We
    66  check not only that they are the same, but that transitions are the
    67  same.
    68  */
    69  func TestAgainstBrzozowski(t *testing.T) {
    70  	num := AtLeast(200)
    71  	for i := 0; i < num; i++ {
    72  		o := randomAutomaton(Random())
    73  		a := minimizeSimple(o)
    74  		b := minimize(a)
    75  		It(t).Should("have same language for %v and %v from %v", a, b, o).
    76  			Verify(sameLanguage(a, b))
    77  		It(t).Should("have same number of states (%v vs %v)", a.numStates(), b.numStates()).
    78  			Verify(a.numStates() == b.numStates())
    79  
    80  		sum1 := 0
    81  		for s := 0; s < a.numStates(); s++ {
    82  			sum1 += a.numTransitions(s)
    83  		}
    84  		sum2 := 0
    85  		for s := 0; s < b.numStates(); s++ {
    86  			sum2 += b.numTransitions(s)
    87  		}
    88  		It(t).Should("have same number of transitions (%v vs %v)", sum1, sum2).
    89  			Verify(sum1 == sum2)
    90  	}
    91  }
    92  
    93  // n^2 space usage in Hopcroft minimization?
    94  func TestMinimizeHuge(t *testing.T) {
    95  	NewRegExpWithFlag("+-*(A|.....|BC)*", NONE).ToAutomaton()
    96  }