queue_test.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package queue
  2. import "testing"
  3. func TestQueueLength(t *testing.T) {
  4. q := New()
  5. if q.Length() != 0 {
  6. t.Error("empty queue length not 0")
  7. }
  8. for i := 0; i < 1000; i++ {
  9. q.Add(i)
  10. if q.Length() != i+1 {
  11. t.Error("adding: queue with", i , "elements has length", q.Length())
  12. }
  13. }
  14. for i := 0; i < 1000; i++ {
  15. q.Remove()
  16. if q.Length() != 1000-i-1 {
  17. t.Error("removing: queue with", 1000-i-i , "elements has length", q.Length())
  18. }
  19. }
  20. }
  21. // General warning: Go's benchmark utility (go test -bench .) increases the number of
  22. // iterations until the benchmarks take a reasonable amount of time to run; memory usage
  23. // is *NOT* considered. On my machine, these benchmarks hit around ~1GB before they've had
  24. // enough, but if you have less than that available and start swapping, then all bets are off.
  25. func BenchmarkQueueSerial(b *testing.B) {
  26. q := New()
  27. for i := 0; i < b.N; i++ {
  28. q.Add(nil)
  29. }
  30. for i := 0; i < b.N; i++ {
  31. q.Remove()
  32. }
  33. }
  34. func BenchmarkQueueTickTock(b *testing.B) {
  35. q := New()
  36. for i := 0; i < b.N; i++ {
  37. q.Add(nil)
  38. q.Remove()
  39. }
  40. }