xref: /llvm-project/compiler-rt/lib/orc/tests/unit/c_api_test.cpp (revision 34fccfb23c47fe5b12bbd8bd970d3ad31d869d62)
11169586dSLang Hames //===-- c_api_test.cpp ----------------------------------------------------===//
21169586dSLang Hames //
31169586dSLang Hames // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
41169586dSLang Hames // See https://llvm.org/LICENSE.txt for license information.
51169586dSLang Hames // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
61169586dSLang Hames //
71169586dSLang Hames //===----------------------------------------------------------------------===//
81169586dSLang Hames //
91169586dSLang Hames // This file is a part of the ORC runtime.
101169586dSLang Hames //
111169586dSLang Hames //===----------------------------------------------------------------------===//
121169586dSLang Hames 
1335ea0c6dSLang Hames #include "orc_rt/c_api.h"
141169586dSLang Hames #include "gtest/gtest.h"
151169586dSLang Hames 
TEST(CAPITest,CWrapperFunctionResultInit)161169586dSLang Hames TEST(CAPITest, CWrapperFunctionResultInit) {
17*34fccfb2SLang Hames   orc_rt_CWrapperFunctionResult R;
18*34fccfb2SLang Hames   orc_rt_CWrapperFunctionResultInit(&R);
191169586dSLang Hames 
201169586dSLang Hames   EXPECT_EQ(R.Size, 0U);
211169586dSLang Hames   EXPECT_EQ(R.Data.ValuePtr, nullptr);
221169586dSLang Hames 
231169586dSLang Hames   // Check that this value isn't treated as an out-of-band error.
24*34fccfb2SLang Hames   EXPECT_EQ(orc_rt_CWrapperFunctionResultGetOutOfBandError(&R), nullptr);
251169586dSLang Hames 
261169586dSLang Hames   // Check that we can dispose of the value.
27*34fccfb2SLang Hames   orc_rt_DisposeCWrapperFunctionResult(&R);
281169586dSLang Hames }
291169586dSLang Hames 
TEST(CAPITest,CWrapperFunctionResultAllocSmall)301169586dSLang Hames TEST(CAPITest, CWrapperFunctionResultAllocSmall) {
311169586dSLang Hames   constexpr size_t SmallAllocSize = sizeof(const char *);
321169586dSLang Hames 
33*34fccfb2SLang Hames   auto R = orc_rt_CWrapperFunctionResultAllocate(SmallAllocSize);
34*34fccfb2SLang Hames   char *DataPtr = orc_rt_CWrapperFunctionResultData(&R);
351169586dSLang Hames 
361169586dSLang Hames   for (size_t I = 0; I != SmallAllocSize; ++I)
371169586dSLang Hames     DataPtr[I] = 0x55 + I;
381169586dSLang Hames 
391169586dSLang Hames   // Check that the inline storage in R.Data.Value contains the expected
401169586dSLang Hames   // sequence.
411169586dSLang Hames   EXPECT_EQ(R.Size, SmallAllocSize);
421169586dSLang Hames   for (size_t I = 0; I != SmallAllocSize; ++I)
431169586dSLang Hames     EXPECT_EQ(R.Data.Value[I], (char)(0x55 + I))
441169586dSLang Hames         << "Unexpected value at index " << I;
451169586dSLang Hames 
461169586dSLang Hames   // Check that this value isn't treated as an out-of-band error.
47*34fccfb2SLang Hames   EXPECT_EQ(orc_rt_CWrapperFunctionResultGetOutOfBandError(&R), nullptr);
481169586dSLang Hames 
49*34fccfb2SLang Hames   // Check that orc_rt_CWrapperFunctionResult(Data|Result|Size) and
50*34fccfb2SLang Hames   // orc_rt_CWrapperFunctionResultGetOutOfBandError behave as expected.
51*34fccfb2SLang Hames   EXPECT_EQ(orc_rt_CWrapperFunctionResultData(&R), R.Data.Value);
52*34fccfb2SLang Hames   EXPECT_EQ(orc_rt_CWrapperFunctionResultSize(&R), SmallAllocSize);
53*34fccfb2SLang Hames   EXPECT_FALSE(orc_rt_CWrapperFunctionResultEmpty(&R));
54*34fccfb2SLang Hames   EXPECT_EQ(orc_rt_CWrapperFunctionResultGetOutOfBandError(&R), nullptr);
551169586dSLang Hames 
561169586dSLang Hames   // Check that we can dispose of the value.
57*34fccfb2SLang Hames   orc_rt_DisposeCWrapperFunctionResult(&R);
581169586dSLang Hames }
591169586dSLang Hames 
TEST(CAPITest,CWrapperFunctionResultAllocLarge)601169586dSLang Hames TEST(CAPITest, CWrapperFunctionResultAllocLarge) {
611169586dSLang Hames   constexpr size_t LargeAllocSize = sizeof(const char *) + 1;
621169586dSLang Hames 
63*34fccfb2SLang Hames   auto R = orc_rt_CWrapperFunctionResultAllocate(LargeAllocSize);
64*34fccfb2SLang Hames   char *DataPtr = orc_rt_CWrapperFunctionResultData(&R);
651169586dSLang Hames 
661169586dSLang Hames   for (size_t I = 0; I != LargeAllocSize; ++I)
671169586dSLang Hames     DataPtr[I] = 0x55 + I;
681169586dSLang Hames 
691169586dSLang Hames   // Check that the inline storage in R.Data.Value contains the expected
701169586dSLang Hames   // sequence.
711169586dSLang Hames   EXPECT_EQ(R.Size, LargeAllocSize);
721169586dSLang Hames   EXPECT_EQ(R.Data.ValuePtr, DataPtr);
731169586dSLang Hames   for (size_t I = 0; I != LargeAllocSize; ++I)
741169586dSLang Hames     EXPECT_EQ(R.Data.ValuePtr[I], (char)(0x55 + I))
751169586dSLang Hames         << "Unexpected value at index " << I;
761169586dSLang Hames 
771169586dSLang Hames   // Check that this value isn't treated as an out-of-band error.
78*34fccfb2SLang Hames   EXPECT_EQ(orc_rt_CWrapperFunctionResultGetOutOfBandError(&R), nullptr);
791169586dSLang Hames 
80*34fccfb2SLang Hames   // Check that orc_rt_CWrapperFunctionResult(Data|Result|Size) and
81*34fccfb2SLang Hames   // orc_rt_CWrapperFunctionResultGetOutOfBandError behave as expected.
82*34fccfb2SLang Hames   EXPECT_EQ(orc_rt_CWrapperFunctionResultData(&R), R.Data.ValuePtr);
83*34fccfb2SLang Hames   EXPECT_EQ(orc_rt_CWrapperFunctionResultSize(&R), LargeAllocSize);
84*34fccfb2SLang Hames   EXPECT_FALSE(orc_rt_CWrapperFunctionResultEmpty(&R));
85*34fccfb2SLang Hames   EXPECT_EQ(orc_rt_CWrapperFunctionResultGetOutOfBandError(&R), nullptr);
861169586dSLang Hames 
871169586dSLang Hames   // Check that we can dispose of the value.
88*34fccfb2SLang Hames   orc_rt_DisposeCWrapperFunctionResult(&R);
891169586dSLang Hames }
901169586dSLang Hames 
TEST(CAPITest,CWrapperFunctionResultFromRangeSmall)911169586dSLang Hames TEST(CAPITest, CWrapperFunctionResultFromRangeSmall) {
921169586dSLang Hames   constexpr size_t SmallAllocSize = sizeof(const char *);
931169586dSLang Hames 
941169586dSLang Hames   char Source[SmallAllocSize];
951169586dSLang Hames   for (size_t I = 0; I != SmallAllocSize; ++I)
961169586dSLang Hames     Source[I] = 0x55 + I;
971169586dSLang Hames 
98*34fccfb2SLang Hames   orc_rt_CWrapperFunctionResult R =
99*34fccfb2SLang Hames       orc_rt_CreateCWrapperFunctionResultFromRange(Source, SmallAllocSize);
1001169586dSLang Hames 
1011169586dSLang Hames   // Check that the inline storage in R.Data.Value contains the expected
1021169586dSLang Hames   // sequence.
1031169586dSLang Hames   EXPECT_EQ(R.Size, SmallAllocSize);
1041169586dSLang Hames   for (size_t I = 0; I != SmallAllocSize; ++I)
1051169586dSLang Hames     EXPECT_EQ(R.Data.Value[I], (char)(0x55 + I))
1061169586dSLang Hames         << "Unexpected value at index " << I;
1071169586dSLang Hames 
1081169586dSLang Hames   // Check that we can dispose of the value.
109*34fccfb2SLang Hames   orc_rt_DisposeCWrapperFunctionResult(&R);
1101169586dSLang Hames }
1111169586dSLang Hames 
TEST(CAPITest,CWrapperFunctionResultFromRangeLarge)1121169586dSLang Hames TEST(CAPITest, CWrapperFunctionResultFromRangeLarge) {
1131169586dSLang Hames   constexpr size_t LargeAllocSize = sizeof(const char *) + 1;
1141169586dSLang Hames 
1151169586dSLang Hames   char Source[LargeAllocSize];
1161169586dSLang Hames   for (size_t I = 0; I != LargeAllocSize; ++I)
1171169586dSLang Hames     Source[I] = 0x55 + I;
1181169586dSLang Hames 
119*34fccfb2SLang Hames   orc_rt_CWrapperFunctionResult R =
120*34fccfb2SLang Hames       orc_rt_CreateCWrapperFunctionResultFromRange(Source, LargeAllocSize);
1211169586dSLang Hames 
1221169586dSLang Hames   // Check that the inline storage in R.Data.Value contains the expected
1231169586dSLang Hames   // sequence.
1241169586dSLang Hames   EXPECT_EQ(R.Size, LargeAllocSize);
1251169586dSLang Hames   for (size_t I = 0; I != LargeAllocSize; ++I)
1261169586dSLang Hames     EXPECT_EQ(R.Data.ValuePtr[I], (char)(0x55 + I))
1271169586dSLang Hames         << "Unexpected value at index " << I;
1281169586dSLang Hames 
1291169586dSLang Hames   // Check that we can dispose of the value.
130*34fccfb2SLang Hames   orc_rt_DisposeCWrapperFunctionResult(&R);
1311169586dSLang Hames }
1321169586dSLang Hames 
TEST(CAPITest,CWrapperFunctionResultFromStringSmall)1331169586dSLang Hames TEST(CAPITest, CWrapperFunctionResultFromStringSmall) {
1341169586dSLang Hames   constexpr size_t SmallAllocSize = sizeof(const char *);
1351169586dSLang Hames 
1361169586dSLang Hames   char Source[SmallAllocSize];
1371169586dSLang Hames   for (size_t I = 0; I != SmallAllocSize - 1; ++I)
1381169586dSLang Hames     Source[I] = 'a' + I;
1391169586dSLang Hames   Source[SmallAllocSize - 1] = '\0';
1401169586dSLang Hames 
141*34fccfb2SLang Hames   orc_rt_CWrapperFunctionResult R =
142*34fccfb2SLang Hames       orc_rt_CreateCWrapperFunctionResultFromString(Source);
1431169586dSLang Hames 
1441169586dSLang Hames   // Check that the inline storage in R.Data.Value contains the expected
1451169586dSLang Hames   // sequence.
1461169586dSLang Hames   EXPECT_EQ(R.Size, SmallAllocSize);
1471169586dSLang Hames   for (size_t I = 0; I != SmallAllocSize - 1; ++I)
1481169586dSLang Hames     EXPECT_EQ(R.Data.Value[I], (char)('a' + I))
1491169586dSLang Hames         << "Unexpected value at index " << I;
1501169586dSLang Hames   EXPECT_EQ(R.Data.Value[SmallAllocSize - 1], '\0')
1511169586dSLang Hames       << "Unexpected value at index " << (SmallAllocSize - 1);
1521169586dSLang Hames 
1531169586dSLang Hames   // Check that we can dispose of the value.
154*34fccfb2SLang Hames   orc_rt_DisposeCWrapperFunctionResult(&R);
1551169586dSLang Hames }
1561169586dSLang Hames 
TEST(CAPITest,CWrapperFunctionResultFromStringLarge)1571169586dSLang Hames TEST(CAPITest, CWrapperFunctionResultFromStringLarge) {
1581169586dSLang Hames   constexpr size_t LargeAllocSize = sizeof(const char *) + 1;
1591169586dSLang Hames 
1601169586dSLang Hames   char Source[LargeAllocSize];
1611169586dSLang Hames   for (size_t I = 0; I != LargeAllocSize - 1; ++I)
1621169586dSLang Hames     Source[I] = 'a' + I;
1631169586dSLang Hames   Source[LargeAllocSize - 1] = '\0';
1641169586dSLang Hames 
165*34fccfb2SLang Hames   orc_rt_CWrapperFunctionResult R =
166*34fccfb2SLang Hames       orc_rt_CreateCWrapperFunctionResultFromString(Source);
1671169586dSLang Hames 
1681169586dSLang Hames   // Check that the inline storage in R.Data.Value contains the expected
1691169586dSLang Hames   // sequence.
1701169586dSLang Hames   EXPECT_EQ(R.Size, LargeAllocSize);
1711169586dSLang Hames   for (size_t I = 0; I != LargeAllocSize - 1; ++I)
1721169586dSLang Hames     EXPECT_EQ(R.Data.ValuePtr[I], (char)('a' + I))
1731169586dSLang Hames         << "Unexpected value at index " << I;
1741169586dSLang Hames   EXPECT_EQ(R.Data.ValuePtr[LargeAllocSize - 1], '\0')
1751169586dSLang Hames       << "Unexpected value at index " << (LargeAllocSize - 1);
1761169586dSLang Hames 
1771169586dSLang Hames   // Check that we can dispose of the value.
178*34fccfb2SLang Hames   orc_rt_DisposeCWrapperFunctionResult(&R);
1791169586dSLang Hames }
1801169586dSLang Hames 
TEST(CAPITest,CWrapperFunctionResultFromOutOfBandError)1811169586dSLang Hames TEST(CAPITest, CWrapperFunctionResultFromOutOfBandError) {
1821169586dSLang Hames   constexpr const char *ErrMsg = "test error message";
183*34fccfb2SLang Hames   orc_rt_CWrapperFunctionResult R =
184*34fccfb2SLang Hames       orc_rt_CreateCWrapperFunctionResultFromOutOfBandError(ErrMsg);
1851169586dSLang Hames 
1861169586dSLang Hames #ifndef NDEBUG
187*34fccfb2SLang Hames   EXPECT_DEATH({ orc_rt_CWrapperFunctionResultData(&R); },
1881169586dSLang Hames                "Cannot get data for out-of-band error value");
189*34fccfb2SLang Hames   EXPECT_DEATH({ orc_rt_CWrapperFunctionResultSize(&R); },
1901169586dSLang Hames                "Cannot get size for out-of-band error value");
1911169586dSLang Hames #endif
1921169586dSLang Hames 
193*34fccfb2SLang Hames   EXPECT_FALSE(orc_rt_CWrapperFunctionResultEmpty(&R));
194*34fccfb2SLang Hames   const char *OOBErrMsg = orc_rt_CWrapperFunctionResultGetOutOfBandError(&R);
1951169586dSLang Hames   EXPECT_NE(OOBErrMsg, nullptr);
1961169586dSLang Hames   EXPECT_NE(OOBErrMsg, ErrMsg);
1971169586dSLang Hames   EXPECT_TRUE(strcmp(OOBErrMsg, ErrMsg) == 0);
1981169586dSLang Hames 
199*34fccfb2SLang Hames   orc_rt_DisposeCWrapperFunctionResult(&R);
2001169586dSLang Hames }
201