You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.1 KiB

  1. package compress
  2. import (
  3. "math/rand"
  4. "testing"
  5. )
  6. func TestSortedData(t *testing.T) {
  7. data := make([]int32, 102400)
  8. for i := 1; i < len(data); i++ {
  9. data[i] = data[i-1] + rand.Int31n(15)
  10. }
  11. testCompressAndUncompress(t, data, "Sorted data")
  12. }
  13. func TestUnsortedData(t *testing.T) {
  14. data := make([]int32, 102400)
  15. for i := 0; i < len(data); i++ {
  16. data[i] = rand.Int31n(255)
  17. }
  18. testCompressAndUncompress(t, data, "Unsorted data")
  19. }
  20. func testCompressAndUncompress(t *testing.T, data []int32, desc string) {
  21. compressed_data, err := Compress32(data)
  22. if err != nil {
  23. t.Fatal("Compress error", err.Error())
  24. }
  25. uncompressed_data, err := Uncompress32(compressed_data, make([]int32, len(data)*2))
  26. if err != nil {
  27. t.Fatal("Compress error", err.Error())
  28. }
  29. if len(uncompressed_data) != len(data) {
  30. t.Fatal("Len differs", len(data), len(uncompressed_data))
  31. }
  32. for i := 0; i < len(data); i++ {
  33. if data[i] != uncompressed_data[i] {
  34. t.Fatal("Data differs:", i, data[i], uncompressed_data[i])
  35. }
  36. }
  37. println(desc, " Data length:", len(data), " => Compressed length:", len(compressed_data))
  38. }