gameboy superoptimizer
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.

94 lines
2.4 KiB

  1. from gbso.cpu.insn import ADD_A_N, ADD_A_R, DEC_HL, LD_RR_NN
  2. from gbso.program.mutate import *
  3. def test_mutate_swap():
  4. trials = 1000
  5. swapped_count = 0
  6. insn_a = ADD_A_R(R8.C)
  7. insn_b = DEC_HL()
  8. prgm = Program(insns=[insn_a, insn_b])
  9. for _ in range(trials):
  10. new_prgm = mutate_swap(prgm)
  11. assert len(new_prgm.insns) == 2
  12. assert isinstance(new_prgm.insns[0], Insn)
  13. assert isinstance(new_prgm.insns[1], Insn)
  14. assert new_prgm.insns == prgm.insns or new_prgm.insns == [insn_b, insn_a]
  15. if new_prgm.insns != prgm.insns:
  16. swapped_count += 1
  17. assert abs(0.5 - (swapped_count / trials)) < 0.05
  18. def test_mutate_insn():
  19. trials = 1000
  20. seen_add_insns = 0
  21. seen_unused = 0
  22. add_insn = ADD_A_N(128)
  23. prgm = Program(insns=[add_insn])
  24. for _ in range(trials):
  25. new_prgm = mutate_insn(prgm, 0.5)
  26. assert len(new_prgm.insns) == 1
  27. assert isinstance(new_prgm.insns[0], Insn)
  28. if new_prgm.insns[0] == add_insn:
  29. seen_add_insns += 1
  30. elif new_prgm.insns[0] == UNUSED():
  31. seen_unused += 1
  32. assert seen_add_insns < 10
  33. assert abs(0.5 - (seen_unused / trials)) < 0.05
  34. def test_create_random_insn():
  35. insn = create_random_insn(ADD_A_R)
  36. assert type(insn) == ADD_A_R
  37. assert type(insn.r) == R8
  38. insn = create_random_insn(ADD_A_N)
  39. assert type(insn) == ADD_A_N
  40. assert type(insn.n) == int
  41. insn = create_random_insn(LD_RR_NN)
  42. assert type(insn) == LD_RR_NN
  43. assert type(insn.rr) == R16
  44. assert type(insn.nn) == int
  45. def test_create_random_operand():
  46. op = create_random_operand(Operand.R8)
  47. assert type(op) == R8
  48. op = create_random_operand(Operand.R16)
  49. assert type(op) == R16
  50. for _ in range(100):
  51. op = create_random_operand(Operand.R16_NO_SP)
  52. assert type(op) == R16
  53. assert op != R16.SP
  54. for _ in range(100):
  55. op = create_random_operand(Operand.IMM3)
  56. assert type(op) == int
  57. assert op >= 0 and op < 8
  58. for _ in range(300):
  59. op = create_random_operand(Operand.IMM8)
  60. assert type(op) == int
  61. assert op >= 0 and op < 256
  62. for _ in range(500):
  63. op = create_random_operand(Operand.IMM16)
  64. assert type(op) == int
  65. assert op >= 0 and op < 65536
  66. for _ in range(300):
  67. op = create_random_operand(Operand.SIMM8)
  68. assert type(op) == int
  69. assert op >= -128 and op < 128