set_test.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package main
  2. import (
  3. "fmt"
  4. "reflect"
  5. "testing"
  6. "time"
  7. )
  8. func generateInput(inputLen int) []string {
  9. var input []string
  10. for i := 0; i < inputLen; i++ {
  11. input = append(input, fmt.Sprintf("s%d", i))
  12. }
  13. return input
  14. }
  15. func testChunkStrings(t *testing.T, inputLen, numChunks int) {
  16. t.Logf("inputLen=%d, numChunks=%d", inputLen, numChunks)
  17. input := generateInput(inputLen)
  18. result := chunkStrings(input, numChunks)
  19. t.Logf("result has %d chunks", len(result))
  20. var inputReconstructedFromResult []string
  21. for i, chunk := range result {
  22. t.Logf("chunk %d has %d elements", i, len(chunk))
  23. inputReconstructedFromResult = append(inputReconstructedFromResult, chunk...)
  24. }
  25. if !reflect.DeepEqual(input, inputReconstructedFromResult) {
  26. t.Fatal("input != inputReconstructedFromResult")
  27. }
  28. }
  29. func TestChunkStrings_4_4(t *testing.T) {
  30. testChunkStrings(t, 4, 4)
  31. }
  32. func TestChunkStrings_4_1(t *testing.T) {
  33. testChunkStrings(t, 4, 1)
  34. }
  35. func TestChunkStrings_1_4(t *testing.T) {
  36. testChunkStrings(t, 1, 4)
  37. }
  38. func TestChunkStrings_1000_8(t *testing.T) {
  39. testChunkStrings(t, 1000, 8)
  40. }
  41. func TestChunkStrings_1000_9(t *testing.T) {
  42. testChunkStrings(t, 1000, 9)
  43. }
  44. func testShuffleStrings(t *testing.T, inputLen int, seed int64) {
  45. t.Logf("inputLen=%d, seed=%d", inputLen, seed)
  46. x := generateInput(inputLen)
  47. shuffleStrings(x, seed)
  48. t.Logf("shuffled: %v", x)
  49. }
  50. func TestShuffleStrings_100(t *testing.T) {
  51. testShuffleStrings(t, 100, time.Now().UnixNano())
  52. }