...

Source file src/net/parse_test.go

Documentation: net

		 1  // Copyright 2009 The Go Authors. All rights reserved.
		 2  // Use of this source code is governed by a BSD-style
		 3  // license that can be found in the LICENSE file.
		 4  
		 5  package net
		 6  
		 7  import (
		 8  	"bufio"
		 9  	"os"
		10  	"runtime"
		11  	"testing"
		12  )
		13  
		14  func TestReadLine(t *testing.T) {
		15  	// /etc/services file does not exist on android, plan9, windows.
		16  	switch runtime.GOOS {
		17  	case "android", "plan9", "windows":
		18  		t.Skipf("not supported on %s", runtime.GOOS)
		19  	}
		20  	filename := "/etc/services" // a nice big file
		21  
		22  	fd, err := os.Open(filename)
		23  	if err != nil {
		24  		t.Fatal(err)
		25  	}
		26  	defer fd.Close()
		27  	br := bufio.NewReader(fd)
		28  
		29  	file, err := open(filename)
		30  	if file == nil {
		31  		t.Fatal(err)
		32  	}
		33  	defer file.close()
		34  
		35  	lineno := 1
		36  	byteno := 0
		37  	for {
		38  		bline, berr := br.ReadString('\n')
		39  		if n := len(bline); n > 0 {
		40  			bline = bline[0 : n-1]
		41  		}
		42  		line, ok := file.readLine()
		43  		if (berr != nil) != !ok || bline != line {
		44  			t.Fatalf("%s:%d (#%d)\nbufio => %q, %v\nnet => %q, %v", filename, lineno, byteno, bline, berr, line, ok)
		45  		}
		46  		if !ok {
		47  			break
		48  		}
		49  		lineno++
		50  		byteno += len(line) + 1
		51  	}
		52  }
		53  
		54  func TestGoDebugString(t *testing.T) {
		55  	defer os.Setenv("GODEBUG", os.Getenv("GODEBUG"))
		56  	tests := []struct {
		57  		godebug string
		58  		key		 string
		59  		want		string
		60  	}{
		61  		{"", "foo", ""},
		62  		{"foo=", "foo", ""},
		63  		{"foo=bar", "foo", "bar"},
		64  		{"foo=bar,", "foo", "bar"},
		65  		{"foo,foo=bar,", "foo", "bar"},
		66  		{"foo1=bar,foo=bar,", "foo", "bar"},
		67  		{"foo=bar,foo=bar,", "foo", "bar"},
		68  		{"foo=", "foo", ""},
		69  		{"foo", "foo", ""},
		70  		{",foo", "foo", ""},
		71  		{"foo=bar,baz", "loooooooong", ""},
		72  	}
		73  	for _, tt := range tests {
		74  		os.Setenv("GODEBUG", tt.godebug)
		75  		if got := goDebugString(tt.key); got != tt.want {
		76  			t.Errorf("for %q, goDebugString(%q) = %q; want %q", tt.godebug, tt.key, got, tt.want)
		77  		}
		78  	}
		79  }
		80  
		81  func TestDtoi(t *testing.T) {
		82  	for _, tt := range []struct {
		83  		in	string
		84  		out int
		85  		off int
		86  		ok	bool
		87  	}{
		88  		{"", 0, 0, false},
		89  		{"0", 0, 1, true},
		90  		{"65536", 65536, 5, true},
		91  		{"123456789", big, 8, false},
		92  		{"-0", 0, 0, false},
		93  		{"-1234", 0, 0, false},
		94  	} {
		95  		n, i, ok := dtoi(tt.in)
		96  		if n != tt.out || i != tt.off || ok != tt.ok {
		97  			t.Errorf("got %d, %d, %v; want %d, %d, %v", n, i, ok, tt.out, tt.off, tt.ok)
		98  		}
		99  	}
	 100  }
	 101  

View as plain text