| // Copyright 2022 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| // [START imports] |
| #include <dirent.h> |
| #include <fcntl.h> |
| #include <fidl/examples.qemuedu/cpp/wire.h> |
| #include <lib/fdio/directory.h> |
| #include <sys/types.h> |
| |
| #include <gtest/gtest.h> |
| // [END imports] |
| |
| // [START main_body] |
| namespace { |
| |
| constexpr char kEduDevicePath[] = "/dev/sys/platform/pt/PCI0/bus/00:06.0_/qemu-edu"; |
| |
| class QemuEduSystemTest : public testing::Test { |
| public: |
| void SetUp() { |
| int device = open(kEduDevicePath, O_RDWR); |
| ASSERT_GE(device, 0); |
| |
| fidl::ClientEnd<examples_qemuedu::Device> client_end; |
| ASSERT_EQ(fdio_get_service_handle(device, client_end.channel().reset_and_get_address()), ZX_OK); |
| device_ = fidl::WireSyncClient(std::move(client_end)); |
| } |
| |
| fidl::WireSyncClient<examples_qemuedu::Device>& device() { return device_; } |
| |
| private: |
| fidl::WireSyncClient<examples_qemuedu::Device> device_; |
| }; |
| |
| TEST_F(QemuEduSystemTest, LivenessCheck) { |
| fidl::WireResult result = device()->LivenessCheck(); |
| ASSERT_EQ(result.status(), ZX_OK); |
| ASSERT_TRUE(result->value()->result); |
| } |
| |
| TEST_F(QemuEduSystemTest, ComputeFactorial) { |
| std::array<uint32_t, 11> kExpected = { |
| 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, |
| }; |
| for (int i = 0; i < kExpected.size(); i++) { |
| fidl::WireResult result = device()->ComputeFactorial(i); |
| ASSERT_EQ(result.status(), ZX_OK); |
| EXPECT_EQ(result->value()->output, kExpected[i]); |
| } |
| } |
| |
| } // namespace |
| // [END main_body] |