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.

vm_test.go 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package intcode
  2. import (
  3. "reflect"
  4. "sync"
  5. "testing"
  6. )
  7. func TestDayTwoSamples(t *testing.T) {
  8. tables := []struct {
  9. given []int
  10. expected []int
  11. }{
  12. {[]int{1, 0, 0, 0, 99}, []int{2, 0, 0, 0, 99}},
  13. {[]int{2, 3, 0, 3, 99}, []int{2, 3, 0, 6, 99}},
  14. {[]int{2, 4, 4, 5, 99, 0}, []int{2, 4, 4, 5, 99, 9801}},
  15. {[]int{1, 1, 1, 4, 99, 5, 6, 0, 99}, []int{30, 1, 1, 4, 2, 5, 6, 0, 99}},
  16. }
  17. for _, table := range tables {
  18. vm := NewVirtualMachine(table.given)
  19. vm.Run()
  20. if !reflect.DeepEqual(table.expected, vm.Memory) {
  21. t.Errorf("Evaluation of %v was incorrect, got: %v, want: %v.", table.given, vm.Memory, table.expected)
  22. }
  23. }
  24. }
  25. func TestDayFiveSamples(t *testing.T) {
  26. tables := []struct {
  27. given []int
  28. input []int
  29. output []int
  30. }{
  31. // Reads then outputs a number
  32. {[]int{3, 0, 4, 0, 99}, []int{123}, []int{123}},
  33. // Using position mode, consider whether the input is equal to 8; output 1 (if it is) or 0 (if it is not).
  34. {[]int{3, 9, 8, 9, 10, 9, 4, 9, 99, -1, 8}, []int{8}, []int{1}},
  35. {[]int{3, 9, 8, 9, 10, 9, 4, 9, 99, -1, 8}, []int{7}, []int{0}},
  36. // Using position mode, consider whether the input is less than 8; output 1 (if it is) or 0 (if it is not).
  37. {[]int{3, 9, 7, 9, 10, 9, 4, 9, 99, -1, 8}, []int{7}, []int{1}},
  38. {[]int{3, 9, 7, 9, 10, 9, 4, 9, 99, -1, 8}, []int{8}, []int{0}},
  39. // Using immediate mode, consider whether the input is equal to 8; output 1 (if it is) or 0 (if it is not).
  40. {[]int{3, 3, 1108, -1, 8, 3, 4, 3, 99}, []int{8}, []int{1}},
  41. {[]int{3, 3, 1108, -1, 8, 3, 4, 3, 99}, []int{9}, []int{0}},
  42. // Using immediate mode, consider whether the input is less than 8; output 1 (if it is) or 0 (if it is not).
  43. {[]int{3, 3, 1107, -1, 8, 3, 4, 3, 99}, []int{0}, []int{1}},
  44. {[]int{3, 3, 1107, -1, 8, 3, 4, 3, 99}, []int{10}, []int{0}},
  45. }
  46. for n, table := range tables {
  47. vm := NewVirtualMachine(table.given)
  48. vm.Input = make(chan int, 1)
  49. vm.Output = make(chan int, 1)
  50. for _, v := range table.input {
  51. vm.Input <- v
  52. }
  53. vm.Run()
  54. for _, v := range table.output {
  55. actual := <-vm.Output
  56. if !reflect.DeepEqual(v, actual) {
  57. t.Errorf("[%d] Wrong output value received for %v, got: %v, want: %v.", n, table.given, actual, v)
  58. }
  59. }
  60. }
  61. }
  62. func TestDayNineSamples(t *testing.T) {
  63. tables := []struct {
  64. given []int
  65. input []int
  66. output []int
  67. }{
  68. // Takes no input and produces a copy of itself as output.
  69. {[]int{109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101, 0, 99}, []int{}, []int{109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101, 0, 99}},
  70. // Should output a 16 digit number.
  71. {[]int{1102, 34915192, 34915192, 7, 4, 7, 99, 0}, []int{}, []int{1219070632396864}},
  72. // Should output the large number in the middle.
  73. {[]int{104, 1125899906842624, 99}, []int{}, []int{1125899906842624}},
  74. }
  75. for _, table := range tables {
  76. vm := NewVirtualMachine(table.given)
  77. vm.Input = make(chan int, 1)
  78. vm.Output = make(chan int, 100)
  79. wg := &sync.WaitGroup{}
  80. wg.Add(1)
  81. go func() {
  82. vm.Run()
  83. wg.Done()
  84. }()
  85. for _, v := range table.input {
  86. vm.Input <- v
  87. }
  88. wg.Wait()
  89. for _, v := range table.output {
  90. actual := <-vm.Output
  91. if !reflect.DeepEqual(v, actual) {
  92. t.Errorf("Wrong output value received for %v, got: %v, want: %v.", table.given, actual, v)
  93. }
  94. }
  95. }
  96. }