Exemple #1
0
func StackSpec(c gospec.Context) {
	stack := NewStack()

	c.Specify("An empty stack", func() {

		c.Specify("is empty", func() {
			c.Expect(stack.Empty(), IsTrue)
		})
		c.Specify("After a push, the stack is no longer empty", func() {
			stack.Push("a push")
			c.Expect(stack.Empty(), IsFalse)
		})
	})

	c.Specify("When objects have been pushed onto a stack", func() {
		stack.Push("pushed first")
		stack.Push("pushed last")

		c.Specify("the object pushed last is popped first", func() {
			poppedFirst := stack.Pop()
			c.Expect(poppedFirst, Equals, "pushed last")
		})
		c.Specify("the object pushed first is popped last", func() {
			stack.Pop()
			poppedLast := stack.Pop()
			c.Expect(poppedLast, Equals, "pushed first")
		})
		c.Specify("After popping all objects, the stack is empty", func() {
			stack.Pop()
			stack.Pop()
			c.Expect(stack.Empty(), IsTrue)
		})
	})
}
Exemple #2
0
func StopwordSpec(c gospec.Context) {
	c.Specify("Should ignore stopwords", func() {
		stopwords := keys(words)
		for token := range channel(stopwords...) {
			c.Expect(token.Backing(), Equals, "should never get here")
		}
	})
}
Exemple #3
0
func SimpleSpec(c gospec.Context) {
	c.Specify("Should tokenize a simple string", func() {
		s := Build()
		tokens := s.Tokenize("Hello world, to all the tests out there!")
		for _, str := range []string{"Hello", "world,", "to", "all", "the", "tests", "out", "there!"} {
			c.Expect((<-tokens).Backing(), Equals, str)
		}
	})
}
Exemple #4
0
func NaiveSpec(c gospec.Context) {
	in := make([]complex128, 8)
	for i := range in {
		in[i] = complex(math.Cos(float64(i)/float64(len(in))*2*math.Pi), 0)
	}
	verify := func(out []complex128, start, stride int, name string) {
		c.Specify(name, func() {
			for i := start; i < len(out); i += stride {
				mag, ang := cmplx.Polar(out[i])
				if i == start+stride || i == len(out)+start-stride {
					c.Expect(mag, IsWithin(1e-9), float64(len(out)/stride)/2)
					if real(out[i]) < 0 {
						if ang < 0 {
							c.Expect(ang, IsWithin(1e-9), -math.Pi)
						} else {
							c.Expect(ang, IsWithin(1e-9), math.Pi)
						}
					} else {
						c.Expect(ang, IsWithin(1e-9), 0.0)
					}
				} else {
					c.Expect(mag, IsWithin(1e-9), 0.0)
				}
			}
		})
	}

	c.Specify("Test basic DFT", func() {
		out := make([]complex128, len(in))

		fft.DFT(in, out, 0, 1)
		verify(out, 0, 1, "Start/Stride 0/1")

		in2 := make([]complex128, 2*len(in))
		out2 := make([]complex128, 2*len(in))
		for i := range in2 {
			in2[i] = in[i/2]
		}
		fft.DFT(in2, out2, 0, 2)
		verify(out2, 0, 2, "Start/Stride 0/2")

		fft.DFT(in2, out2, 1, 2)
		verify(out2, 1, 2, "Start/Stride 1/2")

		in5 := make([]complex128, 5*len(in))
		out5 := make([]complex128, 5*len(in))
		for i := range in5 {
			in5[i] = in[i/5]
		}
		for i := 0; i < 5; i++ {
			fft.DFT(in5, out5, i, 5)
			verify(out5, i, 5, fmt.Sprintf("Start/Stride %d/%d", i, len(in5)/len(in)))
		}
	})
}
Exemple #5
0
func FibSpec(c gospec.Context) {
	fib := NewFib().Sequence(10)

	c.Specify("The first two Fibonacci numbers are 0 and 1", func() {
		c.Expect(fib[0], Equals, 0)
		c.Expect(fib[1], Equals, 1)
	})
	c.Specify("Each remaining number is the sum of the previous two", func() {
		for i := 2; i < len(fib); i++ {
			c.Expect(fib[i], Equals, fib[i-1]+fib[i-2])
		}
	})
}
Exemple #6
0
func StemmerSpec(c gospec.Context) {
	pairs := map[string]string{
		"debugging": "debug",
		"laptop":    "laptop",
		"books":     "book",
		"debugger":  "debugg",
		"winery":    "winery",
	}
	for original, stemmed := range pairs {
		c.Specify(fmt.Sprintf("Should stem %s to %s", original, stemmed), func() {
			c.Expect((<-channel(original)).Backing(), Equals, stemmed)
		})
	}
}
Exemple #7
0
func HuffmanDecodeSpec(c gospec.Context) {
	c.Specify("Basic huffman decode", func() {
		var codebook vorbis.Codebook
		codebook.Entries = make([]vorbis.CodebookEntry, 8)
		codebook.Entries[0].Length = 2
		codebook.Entries[1].Length = 4
		codebook.Entries[2].Length = 4
		codebook.Entries[3].Length = 4
		codebook.Entries[4].Length = 4
		codebook.Entries[5].Length = 2
		codebook.Entries[6].Length = 3
		codebook.Entries[7].Length = 3
		codebook.AssignCodewords()

		v := []uint8{0x5F, 0x6E, 0x2A, 0x00}
		br := vorbis.MakeBitReader(bytes.NewBuffer(v))

		c.Expect(codebook.DecodeScalar(br), Equals, 7)
		c.Expect(codebook.DecodeScalar(br), Equals, 6)
		c.Expect(codebook.DecodeScalar(br), Equals, 5)
		c.Expect(codebook.DecodeScalar(br), Equals, 4)
		c.Expect(codebook.DecodeScalar(br), Equals, 3)
		c.Expect(codebook.DecodeScalar(br), Equals, 2)
		c.Expect(codebook.DecodeScalar(br), Equals, 1)
		c.Expect(codebook.DecodeScalar(br), Equals, 0)
	})
}
func DoubleMetaphoneSpec(c gospec.Context) {
	pairs := map[string]string{
		"debugging": "TPKN",
		"laptop":    "LPTP",
		"books":     "PKS",
		"debugger":  "TPKR",
		"winery":    "ANR",
	}
	for original, stemmed := range pairs {
		c.Specify(fmt.Sprintf("Should encode %s to %s", original, stemmed), func() {
			c.Expect((<-channel(original)).Backing(), Equals, stemmed)
		})
	}

	c.Specify("Should return 2 different encodings for Schmidt", func() {
		ch := channel("Schmidt")
		c.Expect((<-ch).Backing(), Equals, "XMT")
		c.Expect((<-ch).Backing(), Equals, "SMT")
	})
}
Exemple #9
0
func FFTWSpec(c gospec.Context) {
	c.Specify("Check agains fftw", func() {
		N := 2 * 3 * 5 * 7 * 11
		in := make([]complex128, N)
		out_fft := make([]complex128, N)
		out_fftw := make([]complex128, N)
		for i := range in {
			in[i] = complex(float64(i/1000-100), float64(i)/10)
		}

		fftw.PlanDft1d(in, out_fftw, fftw.Forward, fftw.Estimate).Execute()
		fft.FFT(in, out_fft)

		tolerance := 1e-5
		for i := range out_fft {
			c.Expect(real(out_fft[i]), IsWithin(tolerance), real(out_fftw[i]))
			c.Expect(imag(out_fft[i]), IsWithin(tolerance), imag(out_fftw[i]))
		}
	})
}
Exemple #10
0
func BitReaderSpec(c gospec.Context) {
	v := []uint8{1, 1, 3, 0, 0, 9}
	//  00001001 00000000 00000000 00000011 00000001 00000001
	//                                                      1
	//                                           001 0000000
	//       001 00000000 00000000 00000011 00000
	//  00001

	c.Specify("Bitreader reads from an io.Reader properly", func() {
		br := vorbis.MakeBitReader(bytes.NewBuffer(v))
		c.Expect(uint32(0x1), Equals, br.ReadBits(1))
		c.Expect(uint32(0x80), Equals, br.ReadBits(10))
		c.Expect(uint32(0x20000060), Equals, br.ReadBits(32))
		c.Expect(uint32(0x1), Equals, br.ReadBits(5))
	})
}
Exemple #11
0
func StringSetSpec(c gospec.Context) {
	set := new(StringSet)

	c.Specify("An empty set contains nothing", func() {
		c.Expect(set.ToArray(), ContainsExactly, Values())
	})
	c.Specify("Distinct values can be added to a set", func() {
		set.Add("a")
		set.Add("b")
		c.Expect(set.ToArray(), ContainsExactly, Values("a", "b"))
	})
	c.Specify("Duplicate values cannot be added to a set", func() {
		set.Add("a")
		set.Add("a")
		c.Expect(set.ToArray(), ContainsExactly, Values("a"))
	})
}
func ExpectationSyntaxSpec(c gospec.Context) {

	c.Specify("Objects can be compared for equality", func() {
		c.Expect(1, Equals, 1)
		c.Expect("string", Equals, "string")

		// There are some shorthands for commonly used comparisons:
		c.Expect(true, IsTrue)
		c.Expect(false, IsFalse)
		c.Expect(nil, IsNil)
		var typedNilPointerInsideInterfaceValue *os.File
		c.Expect(typedNilPointerInsideInterfaceValue, IsNil)

		// Comparing pointer equality is also possible:
		p1 := &Point2{1, 2}
		p2 := p1
		p3 := &Point2{1, 2}
		c.Expect(p2, IsSame, p1)
		c.Expect(p3, Not(IsSame), p1)

		// Comparing floats for equality is not recommended, because
		// floats are rarely exactly equal. So don't write like this:
		c.Expect(3.141, Equals, 3.141)
		// But instead compare using a delta and write like this:
		c.Expect(3.141, IsWithin(0.001), 3.1415926535)

		// Objects with an "Equals(interface{}) bool" method can be
		// compared for equality. See "point.go" for details of how
		// the Equals(interface{}) method should be written. Special
		// care is needed if the objects are used both as values and
		// as pointers.
		a1 := Point2{1, 2}
		a2 := Point2{1, 2}
		c.Expect(a1, Equals, a2)

		b1 := &Point3{1, 2, 3}
		b2 := &Point3{1, 2, 3}
		c.Expect(b1, Equals, b2)
	})

	c.Specify("All expectations can be negated", func() {
		c.Expect(1, Not(Equals), 2)
		c.Expect("apples", Not(Equals), "oranges")
		c.Expect(new(int), Not(IsNil))
	})

	c.Specify("Boolean expressions can be stated about an object", func() {
		s := "some string"
		c.Expect(s, Satisfies, len(s) >= 10 && len(s) <= 20)
		c.Expect(s, Not(Satisfies), len(s) == 0)
	})

	c.Specify("Custom matchers can be defined for commonly used expressions", func() {
		c.Expect("first string", HasSameLengthAs, "other string")
	})

	c.Specify("Arrays/slices, lists, vectors and channels can be tested for containment", func() {
		array := []string{"one", "two", "three"}
		list := list.New()
		list.PushBack("one")
		list.PushBack("two")
		list.PushBack("three")
		vector := new(vector.Vector)
		vector.Push("one")
		vector.Push("two")
		vector.Push("three")
		channel := make(chan string, 10)
		channel <- "one"
		channel <- "two"
		channel <- "three"
		close(channel)

		c.Expect(array, Contains, "one")
		c.Expect(list, Contains, "two")
		c.Expect(vector, Contains, "three")
		c.Expect(channel, Contains, "three")
		c.Expect(array, Not(Contains), "four")

		c.Expect(list, ContainsAll, Values("two", "one"))
		c.Expect(list, ContainsAny, Values("apple", "orange", "one"))
		c.Expect(list, ContainsExactly, Values("two", "one", "three"))
		c.Expect(list, ContainsInOrder, Values("one", "two", "three"))
		c.Expect(list, ContainsInPartialOrder, Values("one", "three"))
	})
}
// MainSpec is the master specification test for the REST server.
func MainSpec(c gospec.Context) {
	c.Specify("GET /", func() {
		response := GetRequest("/")

		c.Specify("returns a status code of 200", func() {
			c.Expect(response.Code, Equals, 200)
		})

		c.Specify("returns a list of available resources", func() {
			var msg MessageSuccess
			err := json.Unmarshal([]byte(response.Body), &msg)
			c.Expect(err, Equals, nil)
			c.Expect(msg.Msg, Equals, "success")
			c.Expect(len(msg.Results), Equals, 2)
		})
	})

	c.Specify("GET /providers", func() {

		c.Specify("returns 401 unauthorized when Authorization is not provided", func() {
			response := GetRequest("/providers")
			c.Expect(response.Code, Equals, 401)
			c.Expect(response.Header.Get("WWW-Authenticate"), Not(IsNil))
		})

		c.Specify("returns 401 unauthorized when Authorization does not contain two arguments", func() {
			request := createRequest("GET", "/providers")
			request.Header.Add("Authorization", "invalid auth header")
			response := do(request)
			body := getResponseBody(response)
			var msg MessageError
			json.Unmarshal([]byte(body), &msg)

			c.Expect(response.StatusCode, Equals, 401)
			c.Expect(response.Header.Get("WWW-Authenticate"), Not(IsNil))
			c.Expect(msg.Code, Equals, 401)
		})

		c.Specify("returns 401 unauthorized when Authorization does not contain GDS", func() {
			request := createRequest("GET", "/providers")
			request.Header.Add("Authorization", "INVALID onetwothreefour")
			response := do(request)
			body := getResponseBody(response)
			var msg MessageError
			json.Unmarshal([]byte(body), &msg)

			c.Expect(response.StatusCode, Equals, 401)
			c.Expect(response.Header.Get("WWW-Authenticate"), Not(IsNil))
			c.Expect(msg.Code, Equals, 401)
		})

		c.Specify("returns 401 unauthorized when Authorization does not have key:signature format", func() {
			request := createRequest("GET", "/providers")
			request.Header.Add("Authorization", "GDS onetwothreefour")
			response := do(request)
			body := getResponseBody(response)
			var msg MessageError
			json.Unmarshal([]byte(body), &msg)

			c.Expect(response.StatusCode, Equals, 401)
			c.Expect(response.Header.Get("WWW-Authenticate"), Not(IsNil))
			c.Expect(msg.Code, Equals, 401)
		})

		c.Specify("returns 401 unauthorized when key is not a valid username", func() {
			request := createRequest("GET", "/providers")
			request.Header.Add("Authorization", "GDS baduser:signature")
			response := do(request)
			body := getResponseBody(response)
			var msg MessageError
			json.Unmarshal([]byte(body), &msg)

			c.Expect(response.StatusCode, Equals, 401)
			c.Expect(response.Header.Get("WWW-Authenticate"), Not(IsNil))
			c.Expect(msg.Code, Equals, 401)
		})

		c.Specify("returns 401 unauthorized when the signature is not valid", func() {
			request := createRequest("GET", "/providers")
			request.Header.Add("Authorization", "GDS username:signature")
			response := do(request)
			body := getResponseBody(response)
			var msg MessageError
			json.Unmarshal([]byte(body), &msg)

			c.Expect(response.StatusCode, Equals, 401)
			c.Expect(response.Header.Get("WWW-Authenticate"), Not(IsNil))
			c.Expect(msg.Code, Equals, 401)
		})

		c.Specify("returns a list of providers when valid credentials are provided", func() {
			response := GetRequestWithAuth("/providers")
			c.Expect(response.Code, Equals, 200)

			var msg MessageSuccess
			err := json.Unmarshal([]byte(response.Body), &msg)
			c.Expect(err, Equals, nil)
			c.Expect(msg.Msg, Equals, "success")
			c.Expect(len(msg.Results), Equals, 3)
		})
	})
}
Exemple #14
0
func ExecutionModelSpec(c gospec.Context) {

	// "Before block", for example common variables for use in all specs.
	commonVariable := ""

	c.Specify("The following child specs modify the same variable", func() {

		// "Before block", for example initialization for this group of specs.
		commonVariable += "x"

		// All sibling specs (specs which are declared within a common parent)
		// are fully isolated from each other. The following three siblings are
		// executed in parallel, each in its own goroutine, and each of them
		// has its own copy of the local variables declared in its parent specs.
		c.Specify("I modify it, but none of my siblings will know it", func() {
			commonVariable += "1"
		})
		c.Specify("Also I modify it, but none of my siblings will know it", func() {
			commonVariable += "2"
		})
		c.Specify("Also I modify it, but none of my siblings will know it", func() {
			commonVariable += "3"
		})

		// "After block", for example tear down of changes to the file system.
		commonVariable += "y"

		// Depending on which of the previous siblings was executed this time,
		// there are three possible values for the variable:
		c.Expect(commonVariable, Satisfies, commonVariable == "x1y" ||
			commonVariable == "x2y" ||
			commonVariable == "x3y")
	})

	c.Specify("You can nest", func() {
		c.Specify("as many specs", func() {
			c.Specify("as you wish.", func() {
				c.Specify("GoSpec does not impose artificial limits, "+
					"so you can organize your specs freely.", func() {
				})
			})
		})
	})

	c.Specify("The distinction between 'Expect' and 'Assume'", func() {
		// When we have non-trivial test setup code, then it is often useful to
		// explicitly state our assumptions about the state of the system under
		// test, before the body of the test is executed.
		//
		// Otherwise it could happen that the test passes even though the code
		// is broken, or then we get lots of unhelpful error messages from the
		// body of the test, even though the bug was in the test setup.
		//
		// For this use case, GoSpec provides 'Assume' in addition to 'Expect'.
		// Use 'Assume' when the test assumes the correct functioning of some
		// behaviour which is not the focus of the current test:
		//
		// - When an 'Expect' fails, then the child specs are executed normally.
		//
		// - When an 'Assume' fails, then the child specs are NOT executed. This
		//   helps to prevent lots of false alarms from the child specs, when
		//   the real problem was in the test setup.

		// Some very complex test setup code
		input := ""
		for ch := 'a'; ch <= 'c'; ch++ {
			input += string(ch)
		}

		// Uncomment this line to add a bug into the test setup:
		//input += " bug"

		// Uncomment one of the following asserts to see their difference:
		//c.Expect(input, Equals, "abc")
		//c.Assume(input, Equals, "abc")

		c.Specify("When a string is made all uppercase", func() {
			result := strings.ToUpper(input)

			c.Specify("it is all uppercase", func() {
				c.Expect(result, Equals, "ABC")
			})
			c.Specify("its length is not changed", func() {
				c.Expect(len(result), Equals, 3)
			})
		})
	})
}
Exemple #15
0
func SuperstripSpec(c gospec.Context) {
	c.Specify("Should trim whitespace", func() {
		c.Expect((<-channel("  foobar  ")).Backing(), Equals, "foobar")
	})

	c.Specify("Should convert to lowercase", func() {
		c.Expect((<-channel("LIKEABOSS")).Backing(), Equals, "likeaboss")
	})

	c.Specify("Should remove unicode characters", func() {
		c.Expect((<-channel("Hello  世界")).Backing(), Equals, "hello")
	})

	c.Specify("Should remove things that aren't letters or numbers", func() {
		c.Expect((<-channel("like,./';'-=a!@#$^*boss")).Backing(), Equals, "likeaboss")
	})

	c.Specify("Should keep numbers", func() {
		c.Expect((<-channel("f00b4r")).Backing(), Equals, "f00b4r")
	})

	c.Specify("Should not emit empty tokens", func() {
		for token := range channel("foobar", ",./;'") {
			c.Expect(token.Backing(), Equals, "foobar")
		}
	})
}
func FileDependenciesSpec(c gospec.Context) {

	c.Specify("Dependencies area read from a file's imports:", func() {

		c.Specify("no imports", func() {
			deps, _ := GetFileDependencies("file.go", `
				package mypkg
			`)
			c.Expect(deps, ContainsExactly, Values())
		})
		c.Specify("one import", func() {
			deps, _ := GetFileDependencies("file.go", `
				package mypkg
				import "dep1"
			`)
			c.Expect(deps, ContainsExactly, Values("dep1"))
		})
		c.Specify("many imports", func() {
			deps, _ := GetFileDependencies("file.go", `
				package mypkg
				import "dep1"
				import "dep2"
			`)
			c.Expect(deps, ContainsExactly, Values("dep1", "dep2"))
		})
		c.Specify("import blocks", func() {
			deps, _ := GetFileDependencies("file.go", `
				package mypkg
				import (
					"dep1"
					"dep2"
				)
			`)
			c.Expect(deps, ContainsExactly, Values("dep1", "dep2"))
		})
		c.Specify("named import", func() {
			deps, _ := GetFileDependencies("file.go", `
				package mypkg
				import name "dep1"
			`)
			c.Expect(deps, ContainsExactly, Values("dep1"))
		})
		c.Specify(". import", func() {
			deps, _ := GetFileDependencies("file.go", `
				package mypkg
				import . "dep1"
			`)
			c.Expect(deps, ContainsExactly, Values("dep1"))
		})
		c.Specify("_ import", func() {
			deps, _ := GetFileDependencies("file.go", `
				package mypkg
				import _ "dep1"
			`)
			c.Expect(deps, ContainsExactly, Values("dep1"))
		})
	})

	c.Specify("Duplicate imports are reported only once", func() {
		deps, _ := GetFileDependencies("file.go", `
			package mypkg
			import a "dep1"
			import b "dep1"
		`)
		c.Expect(deps, ContainsExactly, Values("dep1"))
	})

	c.Specify("Syntax errors are handled gracefully:", func() {

		c.Specify("missing quotes", func() {
			deps, _ := GetFileDependencies("file.go", `
				package mypkg
				import dep1
			`)
			c.Expect(deps, ContainsExactly, Values())
		})
		c.Specify("garbage after the import", func() {
			deps, _ := GetFileDependencies("file.go", `
				package mypkg
				import "dep1" garbage
			`)
			c.Expect(deps, ContainsExactly, Values())
		})
		c.Specify("empty file", func() {
			deps, _ := GetFileDependencies("file.go", `
			`)
			c.Expect(deps, ContainsExactly, Values())
		})
	})
}
Exemple #17
0
func HelloSpec(c gospec.Context) {

	c.Specify("Says a friendly greeting", func() {
		c.Expect(SayHello("World"), Equals, "Hello, World")
	})
}
Exemple #18
0
func Lookup1Spec(c gospec.Context) {
	c.Expect(vorbis.Lookup1Values(48, 2), Equals, 6)
	c.Expect(vorbis.Lookup1Values(49, 2), Equals, 7)
	c.Expect(vorbis.Lookup1ValuesJava(48, 2), Equals, 6)
	c.Expect(vorbis.Lookup1ValuesJava(49, 2), Equals, 7)
}
Exemple #19
0
func TemporaryDirectorySpec(c gospec.Context) {

	c.Specify("When a temporary directory is created", func() {
		dir, _ := CreateTempDir()
		defer dir.Dispose()

		c.Specify("it exists", func() {
			c.Expect(dir, Satisfies, dir.Exists())
		})
		c.Specify("it is empty", func() {
			files := dir.AllFiles()
			c.Expect(files, Satisfies, len(files) == 0)
		})
		c.Specify("After disposing, the directory no longer exists", func() {
			dir.Dispose()
			c.Expect(dir, Not(Satisfies), dir.Exists())
		})
	})
	c.Specify("When multiple temporary directories are created in parallel", func() {
		dir1, _ := CreateTempDir()
		dir2, _ := CreateTempDir()
		defer dir1.Dispose()
		defer dir2.Dispose()

		c.Specify("they all are different directories", func() {
			c.Expect(dir1.Path(), Not(Equals), dir2.Path())
		})
	})

	c.Specify("When files are put into the directory", func() {
		dir, _ := CreateTempDir()
		defer dir.Dispose()

		dir.WriteFile("file.txt", "file contents")

		c.Specify("then the files are in the directory", func() {
			c.Expect(dir.AllFiles(), Contains, "file.txt")
		})
		c.Specify("and the files have the specified content", func() {
			c.Expect(dir.ReadFile("file.txt"), Equals, "file contents")
		})
		c.Specify("After disposing, the files no longer exist", func() {
			dir.Dispose()
			c.Expect(dir.ReadFile("file.txt"), Equals, "")
		})
	})
	c.Specify("When files are put into subdirectories", func() {
		dir, _ := CreateTempDir()
		defer dir.Dispose()

		dir.WriteFile("subdir/file.txt", "file contents")

		c.Specify("then the subdirectory is created", func() {
			c.Expect(dir.AllFiles(), Contains, "subdir")
		})
		c.Specify("and the files are in the subdirectory", func() {
			c.Expect(dir.AllFiles(), Contains, "subdir/file.txt")
		})
	})
}
Exemple #20
0
func LowercaseSpec(c gospec.Context) {
	c.Specify("Should convert to lowercase", func() {
		c.Expect((<-channel("FOOBAR")).Backing(), Equals, "foobar")
	})
}
Exemple #21
0
func HuffmanAssignmentSpec(c gospec.Context) {
	c.Specify("Basic huffman assignment", func() {
		var codebook vorbis.Codebook
		codebook.Entries = make([]vorbis.CodebookEntry, 8)
		codebook.Entries[0].Length = 2
		codebook.Entries[1].Length = 4
		codebook.Entries[2].Length = 4
		codebook.Entries[3].Length = 4
		codebook.Entries[4].Length = 4
		codebook.Entries[5].Length = 2
		codebook.Entries[6].Length = 3
		codebook.Entries[7].Length = 3
		codebook.AssignCodewords()
		c.Expect(codebook.Entries[0].Codeword, Equals, uint32(0))
		c.Expect(codebook.Entries[1].Codeword, Equals, uint32(4))
		c.Expect(codebook.Entries[2].Codeword, Equals, uint32(5))
		c.Expect(codebook.Entries[3].Codeword, Equals, uint32(6))
		c.Expect(codebook.Entries[4].Codeword, Equals, uint32(7))
		c.Expect(codebook.Entries[5].Codeword, Equals, uint32(2))
		c.Expect(codebook.Entries[6].Codeword, Equals, uint32(6))
		c.Expect(codebook.Entries[7].Codeword, Equals, uint32(7))
	})

	c.Specify("Another huffman assignment", func() {
		var codebook vorbis.Codebook
		codebook.Entries = make([]vorbis.CodebookEntry, 8)
		codebook.Entries[0].Length = 1
		codebook.Entries[1].Length = 3
		codebook.Entries[2].Length = 4
		codebook.Entries[3].Length = 7
		codebook.Entries[4].Length = 2
		codebook.Entries[5].Length = 5
		codebook.Entries[6].Length = 6
		codebook.Entries[7].Length = 7
		codebook.AssignCodewords()
		c.Expect(codebook.Entries[0].Codeword, Equals, uint32(0))
		c.Expect(codebook.Entries[1].Codeword, Equals, uint32(4))
		c.Expect(codebook.Entries[2].Codeword, Equals, uint32(10))
		c.Expect(codebook.Entries[3].Codeword, Equals, uint32(0x58))
		c.Expect(codebook.Entries[4].Codeword, Equals, uint32(3))
		c.Expect(codebook.Entries[5].Codeword, Equals, uint32(0x17))
		c.Expect(codebook.Entries[6].Codeword, Equals, uint32(0x2D))
		c.Expect(codebook.Entries[7].Codeword, Equals, uint32(0x59))
	})

	c.Specify("Large huffman assignment", func() {
		var codebook vorbis.Codebook
		codebook.Entries = make([]vorbis.CodebookEntry, 100)
		lengths := []int{3, 8, 9, 13, 10, 12, 12, 12, 12, 12, 6, 4, 6, 8, 6, 8,
			10, 10, 11, 12, 8, 5, 4, 10, 4, 7, 8, 9, 10, 11, 13, 8, 10, 8, 9, 9,
			11, 12, 13, 14, 10, 6, 4, 9, 3, 5, 6, 8, 10, 11, 11, 8, 6, 9, 5, 5, 6,
			7, 9, 11, 12, 9, 7, 11, 6, 6, 6, 7, 8, 10, 12, 11, 9, 12, 7, 7, 6, 6,
			7, 9, 13, 12, 10, 13, 9, 8, 7, 7, 7, 8, 11, 15, 11, 15, 11, 10, 9, 8,
			7, 7}
		for i := range codebook.Entries {
			codebook.Entries[i].Length = lengths[i]
		}
		codebook.AssignCodewords()
		codewords := []uint32{0x0, 0x20, 0x42, 0x430, 0x87, 0x219, 0x21a, 0x21b,
			0x220, 0x221, 0x9, 0x3, 0xa, 0x23, 0xb, 0x40, 0x89, 0x8a, 0x111,
			0x22c, 0x41, 0x9, 0x5, 0x108, 0x6, 0x22, 0x43, 0x85, 0x109, 0x117,
			0x431, 0x46, 0x11c, 0x70, 0x8f, 0xe2, 0x23a, 0x22d, 0x8ec, 0x11da,
			0x1c6, 0x1d, 0x8, 0xe4, 0x5, 0xf, 0x24, 0x73, 0x1c7, 0x394, 0x395,
			0x94, 0x26, 0x12a, 0x18, 0x19, 0x27, 0x4b, 0x12b, 0x396, 0x477,
			0x1a0, 0x69, 0x397, 0x35, 0x36, 0x37, 0x70, 0xd1, 0x342, 0xd0c, 0x687,
			0x1c4, 0xd0d, 0x72, 0x73, 0x3a, 0x3b, 0x78, 0x1c5, 0x1c60, 0xe31,
			0x38d, 0x1c61, 0x1c7, 0xf2, 0x7a, 0x7b, 0x7c, 0xf3, 0x719, 0x23b6,
			0x7d0, 0x23b7, 0x7d1, 0x3e9, 0x1f5, 0xfb, 0x7e, 0x7f}

		for i := range codewords {
			c.Expect(codebook.Entries[i].Codeword, Equals, codewords[i])
		}
	})

	c.Specify("Codebook with a single zero-bit entry", func() {
		var codebook vorbis.Codebook
		codebook.Entries = make([]vorbis.CodebookEntry, 1)
		codebook.Entries[0].Length = 0
		codebook.AssignCodewords()
		c.Expect(codebook.Entries[0].Codeword, Equals, uint32(0))
	})
}
Exemple #22
0
func TokenizerSpec(c gospec.Context) {
	c.Specify("Should build TokenChan from []string", func() {
		c.Expect((<-NewTokenChan([]string{"foobar"})).Backing(), Equals, "foobar")
	})
}