|
| 1 | +// Copyright (C) 2016 Google Inc. |
| 2 | +// |
| 3 | +// This program is free software; you can redistribute it and/or modify |
| 4 | +// it under the terms of the GNU General Public License as published by |
| 5 | +// the Free Software Foundation; either version 2 of the License, or |
| 6 | +// (at your option) any later version. |
| 7 | +// |
| 8 | +// This program is distributed in the hope that it will be useful, |
| 9 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | +// GNU General Public License for more details. |
| 12 | +// |
| 13 | +// You should have received a copy of the GNU General Public License along |
| 14 | +// with this program; if not, write to the Free Software Foundation, Inc., |
| 15 | +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 16 | +// |
| 17 | +//////////////////////////////////////////////////////////////////////////////// |
| 18 | + |
| 19 | +#include "gmock/gmock.h" |
| 20 | +#include "gtest/gtest.h" |
| 21 | +#include "deepmind/include/deepmind_context.h" |
| 22 | +#include "deepmind/support/test_srcdir.h" |
| 23 | + |
| 24 | +namespace { |
| 25 | + |
| 26 | +using ::deepmind::lab::TestSrcDir; |
| 27 | +using ::testing::ElementsAre; |
| 28 | +using ::testing::ElementsAreArray; |
| 29 | + |
| 30 | +TEST(DeepmindCallbackTest, CreateAndDestroyContext) { |
| 31 | + DeepmindContext ctx{}; |
| 32 | + const char arg0[] = "dmlab"; |
| 33 | + ASSERT_EQ(0, dmlab_create_context(TestSrcDir().c_str(), &ctx)); |
| 34 | + ASSERT_EQ(0, ctx.hooks.set_script_name(ctx.userdata, "tests/callbacks_test")); |
| 35 | + |
| 36 | + ctx.hooks.add_setting(ctx.userdata, "command", "hello"); |
| 37 | + ASSERT_EQ(0, ctx.hooks.init(ctx.userdata)); |
| 38 | + ASSERT_EQ(0, ctx.hooks.start(ctx.userdata, 0, 0)); |
| 39 | + |
| 40 | + const char* cmd_line = ctx.hooks.replace_command_line(ctx.userdata, arg0); |
| 41 | + EXPECT_THAT(cmd_line, ::testing::HasSubstr("hello")); |
| 42 | + EXPECT_STREQ("lt_chasm_1", ctx.hooks.next_map(ctx.userdata)); |
| 43 | + EXPECT_STREQ("lt_chasm_2", ctx.hooks.next_map(ctx.userdata)); |
| 44 | + EXPECT_EQ(1, ctx.hooks.run_lua_snippet( |
| 45 | + ctx.userdata, "return (...):commandLine('') and 1 or 0")); |
| 46 | + dmlab_release_context(&ctx); |
| 47 | +} |
| 48 | + |
| 49 | +TEST(DeepmindCallbackTest, CustomObservations) { |
| 50 | + DeepmindContext ctx{}; |
| 51 | + const char callbacks_test[] = "tests/callbacks_test"; |
| 52 | + const char order[] = "Find Apples!"; |
| 53 | + ASSERT_EQ(0, dmlab_create_context(TestSrcDir().c_str(), &ctx)); |
| 54 | + ctx.hooks.add_setting(ctx.userdata, "order", order); |
| 55 | + ASSERT_EQ(0, ctx.hooks.set_script_name(ctx.userdata, callbacks_test)); |
| 56 | + ASSERT_EQ(0, ctx.hooks.init(ctx.userdata)); |
| 57 | + ASSERT_EQ(3, ctx.hooks.custom_observation_count(ctx.userdata)); |
| 58 | + EnvCApi_ObservationSpec spec; |
| 59 | + |
| 60 | + EXPECT_STREQ("LOCATION", ctx.hooks.custom_observation_name(ctx.userdata, 0)); |
| 61 | + ctx.hooks.custom_observation_spec(ctx.userdata, 0, &spec); |
| 62 | + EXPECT_EQ(1, spec.dims); |
| 63 | + EXPECT_EQ(EnvCApi_ObservationDoubles, spec.type); |
| 64 | + EXPECT_EQ(3, spec.shape[0]); |
| 65 | + |
| 66 | + EXPECT_STREQ("ORDER", ctx.hooks.custom_observation_name(ctx.userdata, 1)); |
| 67 | + ctx.hooks.custom_observation_spec(ctx.userdata, 1, &spec); |
| 68 | + EXPECT_EQ(1, spec.dims); |
| 69 | + EXPECT_EQ(EnvCApi_ObservationBytes, spec.type); |
| 70 | + EXPECT_EQ(0, spec.shape[0]); |
| 71 | + |
| 72 | + EXPECT_STREQ("EPISODE", ctx.hooks.custom_observation_name(ctx.userdata, 2)); |
| 73 | + ctx.hooks.custom_observation_spec(ctx.userdata, 2, &spec); |
| 74 | + EXPECT_EQ(1, spec.dims); |
| 75 | + EXPECT_EQ(EnvCApi_ObservationDoubles, spec.type); |
| 76 | + EXPECT_EQ(1, spec.shape[0]); |
| 77 | + |
| 78 | + const int episode = 10; |
| 79 | + ASSERT_EQ(0, ctx.hooks.start(ctx.userdata, episode, 0)); |
| 80 | + |
| 81 | + EnvCApi_Observation obs; |
| 82 | + ctx.hooks.custom_observation(ctx.userdata, 0, &obs); |
| 83 | + ASSERT_EQ(1, obs.spec.dims); |
| 84 | + ASSERT_EQ(EnvCApi_ObservationDoubles, obs.spec.type); |
| 85 | + EXPECT_THAT(std::make_tuple(obs.payload.doubles, obs.spec.shape[0]), |
| 86 | + ElementsAre(10.0, 20.0, 30.0)); |
| 87 | + |
| 88 | + ctx.hooks.custom_observation(ctx.userdata, 1, &obs); |
| 89 | + ASSERT_EQ(1, obs.spec.dims); |
| 90 | + ASSERT_EQ(EnvCApi_ObservationBytes, obs.spec.type); |
| 91 | + EXPECT_THAT(std::make_tuple(obs.payload.bytes, obs.spec.shape[0]), |
| 92 | + ElementsAreArray(order, sizeof(order) - 1)); |
| 93 | + |
| 94 | + ctx.hooks.custom_observation(ctx.userdata, 2, &obs); |
| 95 | + ASSERT_EQ(1, obs.spec.dims); |
| 96 | + ASSERT_EQ(EnvCApi_ObservationDoubles, obs.spec.type); |
| 97 | + EXPECT_THAT(std::make_tuple(obs.payload.doubles, obs.spec.shape[0]), |
| 98 | + ElementsAre(episode)); |
| 99 | + |
| 100 | + dmlab_release_context(&ctx); |
| 101 | +} |
| 102 | + |
| 103 | +} // namespace |
0 commit comments