llvm-project
492 строки · 16.6 Кб
1//===-- ScalarTest.cpp ----------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "gtest/gtest.h"10
11#include "lldb/Utility/DataExtractor.h"12#include "lldb/Utility/Endian.h"13#include "lldb/Utility/Scalar.h"14#include "lldb/Utility/Status.h"15#include "lldb/Utility/StreamString.h"16#include "lldb/lldb-enumerations.h"17#include "llvm/ADT/APSInt.h"18#include "llvm/Testing/Support/Error.h"19
20#include <algorithm>21#include <cmath>22
23using namespace lldb_private;24using llvm::APFloat;25using llvm::APInt;26using llvm::Failed;27using llvm::Succeeded;28
29template <typename T>30bool checkInequality(T c1, T c2) {31return (Scalar(c1) != Scalar(c2));32}
33
34template <typename T>35bool checkEquality(T c1, T c2) {36return (Scalar(c1) == Scalar(c2));37}
38
39TEST(ScalarTest, Equality) {40ASSERT_TRUE(checkInequality<int>(23, 24));41ASSERT_TRUE(checkEquality<int>(96, 96));42ASSERT_TRUE(checkInequality<float>(4.0f, 4.5f));43ASSERT_TRUE(checkEquality<float>(4.0f, 4.0f));44
45auto apint1 = APInt(64, 234);46auto apint2 = APInt(64, 246);47ASSERT_TRUE(checkInequality<APInt>(apint1, apint2));48ASSERT_TRUE(checkEquality<APInt>(apint1, apint1));49
50Scalar void1;51Scalar void2;52float f1 = 2.0;53ASSERT_TRUE(void1 == void2);54ASSERT_FALSE(void1 == Scalar(f1));55}
56
57TEST(ScalarTest, Comparison) {58auto s1 = Scalar(23);59auto s2 = Scalar(46);60ASSERT_TRUE(s1 < s2);61ASSERT_TRUE(s1 <= s2);62ASSERT_TRUE(s2 > s1);63ASSERT_TRUE(s2 >= s1);64}
65
66TEST(ScalarTest, ComparisonFloat) {67auto s1 = Scalar(23.0f);68auto s2 = Scalar(46.0f);69ASSERT_TRUE(s1 < s2);70ASSERT_TRUE(s1 <= s2);71ASSERT_TRUE(s2 > s1);72ASSERT_TRUE(s2 >= s1);73}
74
75template <typename T> static void CheckConversion(T val) {76SCOPED_TRACE("val = " + std::to_string(val));77EXPECT_EQ((signed char)val, Scalar(val).SChar());78EXPECT_EQ((unsigned char)val, Scalar(val).UChar());79EXPECT_EQ((short)val, Scalar(val).SShort());80EXPECT_EQ((unsigned short)val, Scalar(val).UShort());81EXPECT_EQ((int)val, Scalar(val).SInt());82EXPECT_EQ((unsigned)val, Scalar(val).UInt());83EXPECT_EQ((long)val, Scalar(val).SLong());84EXPECT_EQ((unsigned long)val, Scalar(val).ULong());85EXPECT_EQ((long long)val, Scalar(val).SLongLong());86EXPECT_EQ((unsigned long long)val, Scalar(val).ULongLong());87EXPECT_NEAR((float)val, Scalar(val).Float(), std::abs(val / 1e6));88EXPECT_NEAR((double)val, Scalar(val).Double(), std::abs(val / 1e12));89EXPECT_NEAR((long double)val, Scalar(val).LongDouble(), std::abs(val / 1e12));90}
91
92TEST(ScalarTest, Getters) {93CheckConversion<int>(0x87654321);94CheckConversion<unsigned int>(0x87654321u);95CheckConversion<long>(0x87654321l);96CheckConversion<unsigned long>(0x87654321ul);97CheckConversion<long long>(0x8765432112345678ll);98CheckConversion<unsigned long long>(0x8765432112345678ull);99CheckConversion<float>(42.25f);100CheckConversion<double>(42.25);101CheckConversion<long double>(42.25L);102
103EXPECT_EQ(APInt(128, 1) << 70, Scalar(std::pow(2.0f, 70.0f)).SInt128(APInt()));104EXPECT_EQ(APInt(128, -1, true) << 70,105Scalar(-std::pow(2.0f, 70.0f)).SInt128(APInt()));106EXPECT_EQ(APInt(128, 1) << 70,107Scalar(std::pow(2.0f, 70.0f)).UInt128(APInt()));108EXPECT_EQ(APInt(128, 0), Scalar(-std::pow(2.0f, 70.0f)).UInt128(APInt()));109
110EXPECT_EQ(APInt(128, 1) << 70, Scalar(std::pow(2.0, 70.0)).SInt128(APInt()));111EXPECT_EQ(APInt(128, -1, true) << 70,112Scalar(-std::pow(2.0, 70.0)).SInt128(APInt()));113EXPECT_EQ(APInt(128, 1) << 70, Scalar(std::pow(2.0, 70.0)).UInt128(APInt()));114EXPECT_EQ(APInt(128, 0), Scalar(-std::pow(2.0, 70.0)).UInt128(APInt()));115}
116
117TEST(ScalarTest, RightShiftOperator) {118int a = 0x00001000;119int b = 0xFFFFFFFF;120int c = 4;121Scalar a_scalar(a);122Scalar b_scalar(b);123Scalar c_scalar(c);124ASSERT_EQ(a >> c, a_scalar >> c_scalar);125ASSERT_EQ(b >> c, b_scalar >> c_scalar);126}
127
128TEST(ScalarTest, GetBytes) {129uint8_t Storage[256];130int a = 0x01020304;131long long b = 0x0102030405060708LL;132float c = 1234567.89e32f;133double d = 1234567.89e42;134char e[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};135char f[32] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,13617, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32};137Scalar a_scalar(a);138Scalar b_scalar(b);139Scalar c_scalar(c);140Scalar d_scalar(d);141Scalar e_scalar;142Scalar f_scalar;143DataExtractor e_data(e, sizeof(e), endian::InlHostByteOrder(),144sizeof(void *));145DataExtractor f_data(f, sizeof(f), endian::InlHostByteOrder(),146sizeof(void *));147a_scalar.GetBytes(Storage);148ASSERT_EQ(0, memcmp(&a, Storage, sizeof(a)));149b_scalar.GetBytes(Storage);150ASSERT_EQ(0, memcmp(&b, Storage, sizeof(b)));151c_scalar.GetBytes(Storage);152ASSERT_EQ(0, memcmp(&c, Storage, sizeof(c)));153d_scalar.GetBytes(Storage);154ASSERT_EQ(0, memcmp(&d, Storage, sizeof(d)));155ASSERT_THAT_ERROR(156e_scalar.SetValueFromData(e_data, lldb::eEncodingUint, sizeof(e))157.ToError(),158llvm::Succeeded());159e_scalar.GetBytes(Storage);160ASSERT_EQ(0, memcmp(e, Storage, sizeof(e)));161ASSERT_THAT_ERROR(162f_scalar.SetValueFromData(f_data, lldb::eEncodingUint, sizeof(f))163.ToError(),164llvm::Succeeded());165f_scalar.GetBytes(Storage);166ASSERT_EQ(0, memcmp(f, Storage, sizeof(f)));167}
168
169TEST(ScalarTest, GetData) {170auto get_data = [](llvm::APSInt v) {171DataExtractor data;172Scalar(v).GetData(data);173return data.GetData().vec();174};175
176auto vec = [](std::initializer_list<uint8_t> l) {177std::vector<uint8_t> v(l.begin(), l.end());178if (endian::InlHostByteOrder() == lldb::eByteOrderLittle)179std::reverse(v.begin(), v.end());180return v;181};182
183EXPECT_THAT(184get_data(llvm::APSInt::getMaxValue(/*numBits=*/1, /*Unsigned=*/true)),185vec({0x01}));186
187EXPECT_THAT(188get_data(llvm::APSInt::getMaxValue(/*numBits=*/8, /*Unsigned=*/true)),189vec({0xff}));190
191EXPECT_THAT(192get_data(llvm::APSInt::getMaxValue(/*numBits=*/9, /*Unsigned=*/true)),193vec({0x01, 0xff}));194}
195
196TEST(ScalarTest, SetValueFromData) {197uint8_t a[] = {1, 2, 3, 4};198Scalar s;199ASSERT_THAT_ERROR(200s.SetValueFromData(201DataExtractor(a, sizeof(a), lldb::eByteOrderLittle, sizeof(void *)),202lldb::eEncodingSint, sizeof(a))203.ToError(),204llvm::Succeeded());205EXPECT_EQ(0x04030201, s);206ASSERT_THAT_ERROR(207s.SetValueFromData(208DataExtractor(a, sizeof(a), lldb::eByteOrderBig, sizeof(void *)),209lldb::eEncodingSint, sizeof(a))210.ToError(),211llvm::Succeeded());212EXPECT_EQ(0x01020304, s);213}
214
215TEST(ScalarTest, CastOperations) {216long long a = 0xf1f2f3f4f5f6f7f8LL;217Scalar a_scalar(a);218EXPECT_EQ((signed char)a, a_scalar.SChar());219EXPECT_EQ((unsigned char)a, a_scalar.UChar());220EXPECT_EQ((signed short)a, a_scalar.SShort());221EXPECT_EQ((unsigned short)a, a_scalar.UShort());222EXPECT_EQ((signed int)a, a_scalar.SInt());223EXPECT_EQ((unsigned int)a, a_scalar.UInt());224EXPECT_EQ((signed long)a, a_scalar.SLong());225EXPECT_EQ((unsigned long)a, a_scalar.ULong());226EXPECT_EQ((signed long long)a, a_scalar.SLongLong());227EXPECT_EQ((unsigned long long)a, a_scalar.ULongLong());228
229int a2 = 23;230Scalar a2_scalar(a2);231EXPECT_EQ((float)a2, a2_scalar.Float());232EXPECT_EQ((double)a2, a2_scalar.Double());233EXPECT_EQ((long double)a2, a2_scalar.LongDouble());234
235EXPECT_EQ(std::numeric_limits<unsigned int>::min(), Scalar(-1.0f).UInt());236EXPECT_EQ(std::numeric_limits<unsigned int>::max(), Scalar(1e11f).UInt());237EXPECT_EQ(std::numeric_limits<unsigned long long>::min(),238Scalar(-1.0).ULongLong());239EXPECT_EQ(std::numeric_limits<unsigned long long>::max(),240Scalar(1e22).ULongLong());241
242EXPECT_EQ(std::numeric_limits<int>::min(), Scalar(-1e11f).SInt());243EXPECT_EQ(std::numeric_limits<int>::max(), Scalar(1e11f).SInt());244EXPECT_EQ(std::numeric_limits<long long>::min(), Scalar(-1e22).SLongLong());245EXPECT_EQ(std::numeric_limits<long long>::max(), Scalar(1e22).SLongLong());246}
247
248TEST(ScalarTest, ExtractBitfield) {249uint32_t len = sizeof(long long) * 8;250
251long long a1 = 0xf1f2f3f4f5f6f7f8LL;252long long b1 = 0xff1f2f3f4f5f6f7fLL;253Scalar s_scalar(a1);254ASSERT_TRUE(s_scalar.ExtractBitfield(0, 0));255EXPECT_EQ(s_scalar, a1);256ASSERT_TRUE(s_scalar.ExtractBitfield(len, 0));257EXPECT_EQ(s_scalar, a1);258ASSERT_TRUE(s_scalar.ExtractBitfield(len - 4, 4));259EXPECT_EQ(s_scalar, b1);260
261unsigned long long a2 = 0xf1f2f3f4f5f6f7f8ULL;262unsigned long long b2 = 0x0f1f2f3f4f5f6f7fULL;263Scalar u_scalar(a2);264ASSERT_TRUE(u_scalar.ExtractBitfield(0, 0));265EXPECT_EQ(u_scalar, a2);266ASSERT_TRUE(u_scalar.ExtractBitfield(len, 0));267EXPECT_EQ(u_scalar, a2);268ASSERT_TRUE(u_scalar.ExtractBitfield(len - 4, 4));269EXPECT_EQ(u_scalar, b2);270}
271
272template <typename T> static std::string ScalarGetValue(T value) {273StreamString stream;274Scalar(value).GetValue(stream, false);275return std::string(stream.GetString());276}
277
278TEST(ScalarTest, GetValue) {279EXPECT_EQ("12345", ScalarGetValue<signed short>(12345));280EXPECT_EQ("-12345", ScalarGetValue<signed short>(-12345));281EXPECT_EQ("12345", ScalarGetValue<unsigned short>(12345));282EXPECT_EQ(std::to_string(std::numeric_limits<unsigned short>::max()),283ScalarGetValue(std::numeric_limits<unsigned short>::max()));284
285EXPECT_EQ("12345", ScalarGetValue<signed int>(12345));286EXPECT_EQ("-12345", ScalarGetValue<signed int>(-12345));287EXPECT_EQ("12345", ScalarGetValue<unsigned int>(12345));288EXPECT_EQ(std::to_string(std::numeric_limits<unsigned int>::max()),289ScalarGetValue(std::numeric_limits<unsigned int>::max()));290
291EXPECT_EQ("12345678", ScalarGetValue<signed long>(12345678L));292EXPECT_EQ("-12345678", ScalarGetValue<signed long>(-12345678L));293EXPECT_EQ("12345678", ScalarGetValue<unsigned long>(12345678UL));294EXPECT_EQ(std::to_string(std::numeric_limits<unsigned long>::max()),295ScalarGetValue(std::numeric_limits<unsigned long>::max()));296
297EXPECT_EQ("1234567890123", ScalarGetValue<signed long long>(1234567890123LL));298EXPECT_EQ("-1234567890123",299ScalarGetValue<signed long long>(-1234567890123LL));300EXPECT_EQ("1234567890123",301ScalarGetValue<unsigned long long>(1234567890123ULL));302EXPECT_EQ(std::to_string(std::numeric_limits<unsigned long long>::max()),303ScalarGetValue(std::numeric_limits<unsigned long long>::max()));304}
305
306TEST(ScalarTest, LongLongAssigmentOperator) {307Scalar ull;308ull = std::numeric_limits<unsigned long long>::max();309EXPECT_EQ(std::numeric_limits<unsigned long long>::max(), ull.ULongLong());310
311Scalar sll;312sll = std::numeric_limits<signed long long>::max();313EXPECT_EQ(std::numeric_limits<signed long long>::max(), sll.SLongLong());314}
315
316TEST(ScalarTest, Division) {317Scalar lhs(5.0);318Scalar rhs(2.0);319Scalar r = lhs / rhs;320EXPECT_TRUE(r.IsValid());321EXPECT_EQ(r, Scalar(2.5));322}
323
324TEST(ScalarTest, Promotion) {325Scalar a(47);326EXPECT_TRUE(a.IntegralPromote(64, true));327EXPECT_TRUE(a.IsSigned());328EXPECT_EQ(APInt(64, 47), a.UInt128(APInt()));329
330EXPECT_FALSE(a.IntegralPromote(32, true));331EXPECT_FALSE(a.IntegralPromote(32, false));332EXPECT_TRUE(a.IsSigned());333
334EXPECT_TRUE(a.IntegralPromote(64, false));335EXPECT_FALSE(a.IsSigned());336EXPECT_EQ(APInt(64, 47), a.UInt128(APInt()));337
338EXPECT_FALSE(a.IntegralPromote(64, true));339
340EXPECT_TRUE(a.FloatPromote(APFloat::IEEEdouble()));341EXPECT_EQ(Scalar::e_float, a.GetType());342EXPECT_EQ(47.0, a.Double());343
344EXPECT_FALSE(a.FloatPromote(APFloat::IEEEsingle()));345EXPECT_TRUE(a.FloatPromote(APFloat::x87DoubleExtended()));346EXPECT_EQ(47.0L, a.LongDouble());347}
348
349TEST(ScalarTest, SetValueFromCString) {350Scalar a;351
352EXPECT_THAT_ERROR(353a.SetValueFromCString("1234567890123", lldb::eEncodingUint, 8).ToError(),354Succeeded());355EXPECT_EQ(1234567890123ull, a);356
357EXPECT_THAT_ERROR(358a.SetValueFromCString("-1234567890123", lldb::eEncodingSint, 8).ToError(),359Succeeded());360EXPECT_EQ(-1234567890123ll, a);361
362EXPECT_THAT_ERROR(363a.SetValueFromCString("asdf", lldb::eEncodingSint, 8).ToError(),364Failed());365EXPECT_THAT_ERROR(366a.SetValueFromCString("asdf", lldb::eEncodingUint, 8).ToError(),367Failed());368EXPECT_THAT_ERROR(369a.SetValueFromCString("1234567890123", lldb::eEncodingUint, 4).ToError(),370Failed());371EXPECT_THAT_ERROR(a.SetValueFromCString("123456789012345678901234567890",372lldb::eEncodingUint, 8)373.ToError(),374Failed());375EXPECT_THAT_ERROR(376a.SetValueFromCString("-123", lldb::eEncodingUint, 8).ToError(),377Failed());378EXPECT_THAT_ERROR(379a.SetValueFromCString("-2147483648", lldb::eEncodingSint, 4).ToError(),380Succeeded());381EXPECT_EQ(-2147483648, a);382EXPECT_THAT_ERROR(383a.SetValueFromCString("-2147483649", lldb::eEncodingSint, 4).ToError(),384Failed());385EXPECT_THAT_ERROR(386a.SetValueFromCString("47.25", lldb::eEncodingIEEE754, 4).ToError(),387Succeeded());388EXPECT_EQ(47.25f, a);389EXPECT_THAT_ERROR(390a.SetValueFromCString("asdf", lldb::eEncodingIEEE754, 4).ToError(),391Failed());392}
393
394TEST(ScalarTest, APIntConstructor) {395for (auto &width : {8, 16, 32}) {396Scalar A(APInt(width, 24));397EXPECT_TRUE(A.IsSigned());398EXPECT_EQ(A.GetType(), Scalar::e_int);399EXPECT_EQ(APInt(width, 24), A.UInt128(APInt()));400}401}
402
403TEST(ScalarTest, Scalar_512) {404Scalar Z(APInt(512, 0));405ASSERT_TRUE(Z.IsZero());406Z.MakeUnsigned();407ASSERT_TRUE(Z.IsZero());408
409Scalar S(APInt(512, 2000));410ASSERT_STREQ(S.GetTypeAsCString(), "int");411
412ASSERT_TRUE(S.MakeUnsigned());413EXPECT_EQ(S.GetType(), Scalar::e_int);414EXPECT_FALSE(S.IsSigned());415ASSERT_STREQ(S.GetTypeAsCString(), "int");416EXPECT_EQ(S.GetByteSize(), 64U);417
418ASSERT_TRUE(S.MakeSigned());419EXPECT_EQ(S.GetType(), Scalar::e_int);420EXPECT_TRUE(S.IsSigned());421EXPECT_EQ(S.GetByteSize(), 64U);422}
423
424TEST(ScalarTest, TruncOrExtendTo) {425Scalar S(0xffff);426S.TruncOrExtendTo(12, true);427EXPECT_EQ(S.UInt128(APInt()), APInt(12, 0xfffu));428S.TruncOrExtendTo(20, true);429EXPECT_EQ(S.UInt128(APInt()), APInt(20, 0xfffffu));430S.TruncOrExtendTo(24, false);431EXPECT_EQ(S.UInt128(APInt()), APInt(24, 0x0fffffu));432S.TruncOrExtendTo(16, false);433EXPECT_EQ(S.UInt128(APInt()), APInt(16, 0xffffu));434}
435
436TEST(ScalarTest, APFloatConstructor) {437llvm::APFloat my_single(llvm::APFloatBase::IEEEsingle(), "3.14159");438llvm::APFloat my_double(llvm::APFloatBase::IEEEdouble(), "3.14159");439Scalar S(my_single);440Scalar D(my_double);441
442EXPECT_EQ(S.GetType(), Scalar::e_float);443EXPECT_EQ(D.GetType(), Scalar::e_float);444ASSERT_TRUE(S != D);445}
446
447TEST(ScalarTest, CreateAPFloats) {448llvm::APFloat ap_float(llvm::APFloatBase::IEEEsingle(), "3.14159");449llvm::APFloat ap_nan = llvm::APFloat::getNaN(llvm::APFloat::IEEEsingle());450llvm::APSInt int1("12");451llvm::APSInt int2("-4");452Scalar I1(int1);453Scalar I2(int2);454Scalar F(ap_float);455
456llvm::APFloat out1_float = I1.CreateAPFloatFromAPSInt(lldb::eBasicTypeFloat);457llvm::APFloat out1_double =458I1.CreateAPFloatFromAPSInt(lldb::eBasicTypeDouble);459llvm::APFloat out1_longdouble =460I1.CreateAPFloatFromAPSInt(lldb::eBasicTypeLongDouble);461llvm::APFloat out1_nan =462I1.CreateAPFloatFromAPSInt(lldb::eBasicTypeFloatComplex);463EXPECT_TRUE(!out1_float.isNegative());464EXPECT_TRUE(!out1_double.isNegative());465EXPECT_TRUE(out1_double.bitwiseIsEqual(out1_longdouble));466EXPECT_FALSE(out1_double.bitwiseIsEqual(out1_float));467EXPECT_TRUE(out1_nan.bitwiseIsEqual(ap_nan));468
469llvm::APFloat out2_float = I2.CreateAPFloatFromAPSInt(lldb::eBasicTypeFloat);470llvm::APFloat out2_double =471I2.CreateAPFloatFromAPSInt(lldb::eBasicTypeDouble);472llvm::APFloat out2_longdouble =473I2.CreateAPFloatFromAPSInt(lldb::eBasicTypeLongDouble);474llvm::APFloat out2_nan =475I2.CreateAPFloatFromAPSInt(lldb::eBasicTypeFloatComplex);476EXPECT_TRUE(out2_float.isNegative());477EXPECT_TRUE(out2_double.isNegative());478EXPECT_TRUE(out2_double.bitwiseIsEqual(out2_longdouble));479EXPECT_FALSE(out2_double.bitwiseIsEqual(out2_float));480EXPECT_TRUE(out2_nan.bitwiseIsEqual(ap_nan));481
482llvm::APFloat out3_float = F.CreateAPFloatFromAPFloat(lldb::eBasicTypeFloat);483llvm::APFloat out3_double =484F.CreateAPFloatFromAPFloat(lldb::eBasicTypeDouble);485llvm::APFloat out3_longdouble =486F.CreateAPFloatFromAPFloat(lldb::eBasicTypeLongDouble);487llvm::APFloat out3_nan =488F.CreateAPFloatFromAPFloat(lldb::eBasicTypeFloatComplex);489EXPECT_TRUE(out3_double.bitwiseIsEqual(out3_longdouble));490EXPECT_FALSE(out3_double.bitwiseIsEqual(out3_float));491EXPECT_TRUE(out3_nan.bitwiseIsEqual(ap_nan));492}
493