...

Source file src/github.com/soheilhy/cmux/patricia_test.go

Documentation: github.com/soheilhy/cmux

     1  // Copyright 2016 The CMux Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
    12  // implied. See the License for the specific language governing
    13  // permissions and limitations under the License.
    14  
    15  package cmux
    16  
    17  import (
    18  	"strings"
    19  	"testing"
    20  )
    21  
    22  func testPTree(t *testing.T, strs ...string) {
    23  	pt := newPatriciaTreeString(strs...)
    24  	for _, s := range strs {
    25  		if !pt.match(strings.NewReader(s)) {
    26  			t.Errorf("%s is not matched by %s", s, s)
    27  		}
    28  
    29  		if !pt.matchPrefix(strings.NewReader(s + s)) {
    30  			t.Errorf("%s is not matched as a prefix by %s", s+s, s)
    31  		}
    32  
    33  		if pt.match(strings.NewReader(s + s)) {
    34  			t.Errorf("%s matches %s", s+s, s)
    35  		}
    36  
    37  		// The following tests are just to catch index out of
    38  		// range and off-by-one errors and not the functionality.
    39  		pt.matchPrefix(strings.NewReader(s[:len(s)-1]))
    40  		pt.match(strings.NewReader(s[:len(s)-1]))
    41  		pt.matchPrefix(strings.NewReader(s + "$"))
    42  		pt.match(strings.NewReader(s + "$"))
    43  	}
    44  }
    45  
    46  func TestPatriciaOnePrefix(t *testing.T) {
    47  	testPTree(t, "prefix")
    48  }
    49  
    50  func TestPatriciaNonOverlapping(t *testing.T) {
    51  	testPTree(t, "foo", "bar", "dummy")
    52  }
    53  
    54  func TestPatriciaOverlapping(t *testing.T) {
    55  	testPTree(t, "foo", "far", "farther", "boo", "ba", "bar")
    56  }
    57  

View as plain text