utils_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package main
  2. import (
  3. "testing"
  4. "time"
  5. )
  6. func TestByte(t *testing.T) {
  7. table_int32 := map[int32][4]byte{
  8. 0x12345678: {0x12, 0x34, 0x56, 0x78},
  9. }
  10. for i32, b4 := range table_int32 {
  11. out := Int32toByteArray(i32)
  12. if out != b4 {
  13. t.Errorf("Expected %v, got %v\n", b4, out)
  14. }
  15. }
  16. table_int64 := map[int64][8]byte{
  17. 0x0123456789123456: {0x01, 0x23, 0x45, 0x67, 0x89, 0x12, 0x34, 0x56},
  18. }
  19. for i64, b8 := range table_int64 {
  20. out := Int64toByteArray(i64)
  21. if out != b8 {
  22. t.Errorf("Expected %v, got %v\n", b8, out)
  23. }
  24. }
  25. }
  26. func TestWord(t *testing.T) {
  27. table := map[string][][]int{
  28. "this is a test": {{0, 4}, {5, 7}, {8, 9}, {10, 14}},
  29. }
  30. for s, e := range table {
  31. out := find_words(s)
  32. if len(out) != len(e) {
  33. t.Errorf("Expected %d, got %d\n", len(e), len(out))
  34. } else {
  35. for x := range e {
  36. ex := e[x]
  37. ox := out[x]
  38. if (ex[0] != ox[0]) || (ex[1] != ox[1]) {
  39. t.Errorf("Expected %v, got %v\n", ex, ox)
  40. }
  41. }
  42. }
  43. }
  44. }
  45. func TestFormat(t *testing.T) {
  46. dt := time.Date(2022, 11, 25, 4, 5, 6, 0, time.Local)
  47. table := map[string]string{
  48. "2006-01-02": "2022-11-25",
  49. "January 2, 2006": "November 25, 2022",
  50. }
  51. for f, e := range table {
  52. out := FormatDate(dt.Unix(), f)
  53. if out != e {
  54. t.Errorf("Expected %s, got %s\n", e, out)
  55. }
  56. }
  57. }
  58. func TestAbs(t *testing.T) {
  59. var table = [][2]int{{-5, 5}, {-1, 1}, {5, 5}, {0, 0}, {-1000, 1000}}
  60. for _, i := range table {
  61. g := Abs(i[0])
  62. if g != i[1] {
  63. t.Errorf("Abs expected %d, got %d\n", i[1], g)
  64. }
  65. }
  66. }