|
2 | 2 | #include <scratchcpp/list.h> |
3 | 3 | #include <scratchcpp/script.h> |
4 | 4 | #include <enginemock.h> |
| 5 | +#include <randomgeneratormock.h> |
5 | 6 |
|
| 7 | +#include "engine/virtualmachine_p.h" |
6 | 8 | #include "engine/internal/engine.h" |
| 9 | +#include "engine/internal/randomgenerator.h" |
7 | 10 | #include "../common.h" |
8 | 11 |
|
9 | 12 | using namespace libscratchcpp; |
10 | 13 | using namespace vm; |
11 | 14 |
|
| 15 | +using ::testing::Return; |
| 16 | + |
12 | 17 | TEST(VirtualMachineTest, Constructors) |
13 | 18 | { |
14 | 19 | VirtualMachine vm1; |
@@ -867,11 +872,19 @@ TEST(VirtualMachineTest, OP_LIST_DEL) |
867 | 872 | list1.push_back("h"); |
868 | 873 | List *lists[] = { &list1 }; |
869 | 874 |
|
| 875 | + RandomGeneratorMock rng; |
| 876 | + VirtualMachinePrivate::rng = &rng; |
| 877 | + |
870 | 878 | VirtualMachine vm; |
871 | 879 | vm.setBytecode(bytecode); |
872 | 880 | vm.setConstValues(constValues); |
873 | 881 | vm.setLists(lists); |
| 882 | + |
| 883 | + EXPECT_CALL(rng, randint(1, 4)).WillOnce(Return(2)); |
874 | 884 | vm.run(); |
| 885 | + |
| 886 | + VirtualMachinePrivate::rng = RandomGenerator::instance().get(); |
| 887 | + |
875 | 888 | ASSERT_EQ(vm.registerCount(), 14); |
876 | 889 | ASSERT_EQ(vm.getInput(0, 14)->toString(), "a b d e f g h"); |
877 | 890 | ASSERT_EQ(vm.getInput(1, 14)->toString(), "b d e f g h"); |
@@ -931,11 +944,19 @@ TEST(VirtualMachineTest, OP_LIST_INSERT) |
931 | 944 | list1.push_back("h"); |
932 | 945 | List *lists[] = { &list1 }; |
933 | 946 |
|
| 947 | + RandomGeneratorMock rng; |
| 948 | + VirtualMachinePrivate::rng = &rng; |
| 949 | + |
934 | 950 | VirtualMachine vm; |
935 | 951 | vm.setBytecode(bytecode); |
936 | 952 | vm.setConstValues(constValues); |
937 | 953 | vm.setLists(lists); |
| 954 | + |
| 955 | + EXPECT_CALL(rng, randint(1, 12)).WillOnce(Return(5)); |
938 | 956 | vm.run(); |
| 957 | + |
| 958 | + VirtualMachinePrivate::rng = RandomGenerator::instance().get(); |
| 959 | + |
939 | 960 | ASSERT_EQ(vm.registerCount(), 13); |
940 | 961 | ASSERT_EQ(vm.getInput(0, 13)->toString(), "a b new item c d e f g h"); |
941 | 962 | ASSERT_EQ(vm.getInput(1, 13)->toString(), "new item a b new item c d e f g h"); |
@@ -976,11 +997,19 @@ TEST(VirtualMachineTest, OP_LIST_REPLACE) |
976 | 997 | list1.push_back("h"); |
977 | 998 | List *lists[] = { &list1 }; |
978 | 999 |
|
| 1000 | + RandomGeneratorMock rng; |
| 1001 | + VirtualMachinePrivate::rng = &rng; |
| 1002 | + |
979 | 1003 | VirtualMachine vm; |
980 | 1004 | vm.setBytecode(bytecode); |
981 | 1005 | vm.setConstValues(constValues); |
982 | 1006 | vm.setLists(lists); |
| 1007 | + |
| 1008 | + EXPECT_CALL(rng, randint(1, 8)).WillOnce(Return(7)); |
983 | 1009 | vm.run(); |
| 1010 | + |
| 1011 | + VirtualMachinePrivate::rng = RandomGenerator::instance().get(); |
| 1012 | + |
984 | 1013 | ASSERT_EQ(vm.registerCount(), 13); |
985 | 1014 | ASSERT_EQ(vm.getInput(0, 13)->toString(), "a b new item d e f g h"); |
986 | 1015 | ASSERT_EQ(vm.getInput(1, 13)->toString(), "new item b new item d e f g h"); |
@@ -1016,11 +1045,19 @@ TEST(VirtualMachineTest, OP_LIST_GET_ITEM) |
1016 | 1045 | list1.push_back("h"); |
1017 | 1046 | List *lists[] = { &list1 }; |
1018 | 1047 |
|
| 1048 | + RandomGeneratorMock rng; |
| 1049 | + VirtualMachinePrivate::rng = &rng; |
| 1050 | + |
1019 | 1051 | VirtualMachine vm; |
1020 | 1052 | vm.setBytecode(bytecode); |
1021 | 1053 | vm.setConstValues(constValues); |
1022 | 1054 | vm.setLists(lists); |
| 1055 | + |
| 1056 | + EXPECT_CALL(rng, randint(1, 8)).WillOnce(Return(1)); |
1023 | 1057 | vm.run(); |
| 1058 | + |
| 1059 | + VirtualMachinePrivate::rng = RandomGenerator::instance().get(); |
| 1060 | + |
1024 | 1061 | ASSERT_EQ(vm.registerCount(), 13); |
1025 | 1062 | ASSERT_EQ(vm.getInput(0, 13)->toString(), "c"); |
1026 | 1063 | ASSERT_EQ(vm.getInput(1, 13)->toString(), "a"); |
|
0 commit comments