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 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package intcode
  2. import (
  3. "reflect"
  4. "testing"
  5. )
  6. func TestDayTwoSamples(t *testing.T) {
  7. tables := []struct {
  8. given []int
  9. expected []int
  10. }{
  11. {[]int{1, 0, 0, 0, 99}, []int{2, 0, 0, 0, 99}},
  12. {[]int{2, 3, 0, 3, 99}, []int{2, 3, 0, 6, 99}},
  13. {[]int{2, 4, 4, 5, 99, 0}, []int{2, 4, 4, 5, 99, 9801}},
  14. {[]int{1, 1, 1, 4, 99, 5, 6, 0, 99}, []int{30, 1, 1, 4, 2, 5, 6, 0, 99}},
  15. }
  16. for _, table := range tables {
  17. vm := NewVirtualMachine(table.given, false)
  18. vm.Run()
  19. if !reflect.DeepEqual(table.expected, vm.Memory) {
  20. t.Errorf("Evaluation of %v was incorrect, got: %v, want: %v.", table.given, vm.Memory, table.expected)
  21. }
  22. }
  23. }
  24. func TestDayFiveSamples(t *testing.T) {
  25. tables := []struct {
  26. given []int
  27. input []int
  28. output []int
  29. }{
  30. // Reads then outputs a number
  31. {[]int{3, 0, 4, 0, 99}, []int{123}, []int{123}},
  32. // Using position mode, consider whether the input is equal to 8; output 1 (if it is) or 0 (if it is not).
  33. {[]int{3, 9, 8, 9, 10, 9, 4, 9, 99, -1, 8}, []int{8}, []int{1}},
  34. {[]int{3, 9, 8, 9, 10, 9, 4, 9, 99, -1, 8}, []int{7}, []int{0}},
  35. // Using position mode, consider whether the input is less than 8; output 1 (if it is) or 0 (if it is not).
  36. {[]int{3, 9, 7, 9, 10, 9, 4, 9, 99, -1, 8}, []int{7}, []int{1}},
  37. {[]int{3, 9, 7, 9, 10, 9, 4, 9, 99, -1, 8}, []int{8}, []int{0}},
  38. // Using immediate mode, consider whether the input is equal to 8; output 1 (if it is) or 0 (if it is not).
  39. {[]int{3, 3, 1108, -1, 8, 3, 4, 3, 99}, []int{8}, []int{1}},
  40. {[]int{3, 3, 1108, -1, 8, 3, 4, 3, 99}, []int{9}, []int{0}},
  41. // Using immediate mode, consider whether the input is less than 8; output 1 (if it is) or 0 (if it is not).
  42. {[]int{3, 3, 1107, -1, 8, 3, 4, 3, 99}, []int{0}, []int{1}},
  43. {[]int{3, 3, 1107, -1, 8, 3, 4, 3, 99}, []int{10}, []int{0}},
  44. }
  45. for _, table := range tables {
  46. vm := NewVirtualMachine(table.given, true)
  47. for _, v := range table.input {
  48. vm.Input <- v
  49. }
  50. vm.Run()
  51. for _, v := range table.output {
  52. actual := <-vm.Output
  53. if !reflect.DeepEqual(v, actual) {
  54. t.Errorf("Wrong output value received for %v, got: %v, want: %v.", table.given, actual, v)
  55. }
  56. }
  57. }
  58. }