c - 将 C 风格的整数数组传递给 Ada 函数以检索数组中的第一个元素

标签 c googletest ada

我正在尝试将 google test 链接到 ada95 进行单元测试。我知道 ada 有 aunit 和 ahven,但这只是为了看看这是否可能并且超出了我的问题范围。我已经成功地能够使用基本数据类型执行简单的函数和过程。我想尝试做的下一件事类似于以下内容:
这是 main.cpp 文件:

#include <stdio.h>
#include <gtest/gtest.h>

extern "C" {
  int firstElement(int buffer[]);
}

TEST(tryTest, checkBuffer){
   int buffer[10] = {10,1,6,4,3,2,1,3,4,6};
   ASSERT_EQ(buffer[0],firstElement(buffer));
}

int main(int argc, char ** argv) {
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}
为简单起见,我将只放置广告文件:
Lib_test.ads
with Interfaces.C;
with Interfaces.C.Pointers;
package Lib_Test is

  function FirstElement(a: Interfaces.C.Pointers) return Interfaces.C.Int;
  pragma Export(C,FirstElement,"firstElement");
end Lib_Test;
我知道在 c 中,您传递的是指向第一个元素的指针,而不是函数的整个数组。这就是为什么我尝试使用 Interfaces.C.Pointers 作为数据类型,但出现以下错误
subtype mark required in this context
found "Pointers" declared at i-cpoint.ads:44
除了 char 数组,我还没有找到使用其他数组类型的好例子。有人可以告诉我如何将 Interfaces.C.Pointers 用于整数数组,甚至如何解决这个问题,我相信这只是函数参数中的数据类型。我希望能够访问 ada 函数中的 c 整数数组。
谢谢你们!

最佳答案

根据 RM B.3 (70) :

An Ada parameter of an array type with component type T, of any mode, is passed as a t* argument to a C function, where t is the C type corresponding to the Ada type T.


因此,无需使用包 Interfaces.C.Pointers .您可以只使用 Ada 数组类型。一个小例子:
main.cpp
#include <stdio.h>
#include <gtest/gtest.h>

extern "C" {
  void testinit();
  void testfinal();
  int firstElement(int *buffer);
}

class MyTest : public ::testing::Test {
protected:

  MyTest() {
    testinit();          // Initialize the Ada library
  }

  ~MyTest() override {
    testfinal();         // Finalize the Ada library
  }
};

TEST_F(MyTest, CheckBuffer) {
  int buffer[10] = {10,1,6,4,3,2,1,3,4,6};
  ASSERT_EQ(buffer[0], firstElement(buffer));
}

int main(int argc, char **argv) {
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}
test.gpr
with "libtest";

project Test is

   for Source_Dirs use ("src");
   for Object_Dir use "obj";
   for Main use ("main.cpp");
   for Languages use ("C++");

   package Compiler is
      for Switches ("c++") use ("-I/usr/src/googletest/googletest/include");
   end Compiler;

   package Linker is
      for Switches ("c++") use ("-lgtest", "-lgtest_main", "-pthread", "-ltest");
   end Linker;

end Test;
lib_test.ads
with Interfaces.C;

package Lib_Test is

   package C renames Interfaces.C;
   
   type Seq is array (0 .. 9) of C.Int;
      
   function First_Element (A : Seq) return C.Int;
   pragma Export (C, First_Element, "firstElement");
   
end Lib_Test;
lib_test.adb
package body Lib_Test is
   
   -------------------
   -- First_Element --
   -------------------
   
   function First_Element (A : Seq) return C.Int is
   begin
      return A (A'First);
   end First_Element;

end Lib_Test;
libtest.gpr
library project Libtest is
   for Library_Kind use "dynamic";
   for Library_Name use "test";
   for Library_Interface use ("lib_test");
   for Library_Auto_Init use "False";
   for Library_Dir use "lib";
   for Object_Dir use "obj";
   for Source_Dirs use ("src");   
end Libtest;
输出
$ ./obj/main 
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from MyTest
[ RUN      ] MyTest.CheckBuffer
[       OK ] MyTest.CheckBuffer (0 ms)
[----------] 1 test from MyTest (0 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (0 ms total)
[  PASSED  ] 1 test.

关于c - 将 C 风格的整数数组传递给 Ada 函数以检索数组中的第一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62585930/

相关文章:

c++ - 如何在 Google Mock 中使用可选参数模拟方法?

ada - STM32F4 UART 半字寻址

无法使用 C 中的套接字下载多个文件

c - 算术赋值运算符 - 左侧仅计算一次

c - 在C中获取文件最后一行的字符

c++ - CMake 生成的 Xcode 项目无法编译

c - 在二维矩阵(水平、垂直、对角线)中查找回文

c++ - 如何注销测试用例以及如何将特定测试用例注册到 Gtest

gcc - 将 math.h 与 gprbuild 链接起来

generics - Ada : invalid prefix in selected component