diff options
author | Tor Lillqvist <tml@collabora.com> | 2023-03-16 15:32:10 +0200 |
---|---|---|
committer | Tor Lillqvist <tml@collabora.com> | 2023-03-17 11:46:36 +0000 |
commit | e1c6f36d8bcc0799281e3a7e244175f682d97cb2 (patch) | |
tree | 3218e006a6abe07260b3248621012b0ae5fa5864 /cppu | |
parent | f92721bf182952be88b0349a17e46b684d630c29 (diff) |
Add a to_string() function to the code generated for UNO IDL constant groups
If the inpt matches one of the constants exactly, the result is the
name of that constant. If the input matches some combination of
constant values that are single bits, the result is the sequence of
the names of those constants joined with plus signs.
For instance, if the IDL has:
constants Constants {
const byte BIT0 = 1;
const byte BIT1 = 2;
const byte BIT2 = 4;
const byte BIT3 = 8;
}
The result of Constants::to_string(5) is "BIT0+BIT2", and the result
of Constants::to_string(17) is "17".
I am sure there are corner cases that aren't handled as would be
intuitive, especially with types that include unsigned values.
Correspondingly, the semantics of the generated to_string() functions
is not formally defined.
Also add a unit test for the new functionality.
Change-Id: I14aa826d0989ac6dfe97dd5c09119b1601c65643
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/148995
Tested-by: Jenkins
Reviewed-by: Tor Lillqvist <tml@collabora.com>
Diffstat (limited to 'cppu')
-rw-r--r-- | cppu/qa/cppumaker/test_cppumaker.cxx | 32 | ||||
-rw-r--r-- | cppu/qa/cppumaker/types.idl | 54 |
2 files changed, 86 insertions, 0 deletions
diff --git a/cppu/qa/cppumaker/test_cppumaker.cxx b/cppu/qa/cppumaker/test_cppumaker.cxx index a6d3e4942f06..cec02d3c710e 100644 --- a/cppu/qa/cppumaker/test_cppumaker.cxx +++ b/cppu/qa/cppumaker/test_cppumaker.cxx @@ -352,6 +352,9 @@ #include <test/codemaker/cppumaker/TestException1.hpp> #include <test/codemaker/cppumaker/TestException2.hpp> #include <test/codemaker/cppumaker/Constants.hpp> +#include <test/codemaker/cppumaker/ByteBits.hpp> +#include <test/codemaker/cppumaker/ShortBits.hpp> +#include <test/codemaker/cppumaker/UnsignedHyperBits.hpp> #include <memory> #include <com/sun/star/uno/Any.hxx> @@ -390,11 +393,14 @@ public: void testConstants(); + void testSymbolicConstants(); + CPPUNIT_TEST_SUITE(Test); CPPUNIT_TEST(testBigStruct); CPPUNIT_TEST(testPolyStruct); CPPUNIT_TEST(testExceptions); CPPUNIT_TEST(testConstants); + CPPUNIT_TEST(testSymbolicConstants); CPPUNIT_TEST_SUITE_END(); }; @@ -550,6 +556,32 @@ void Test::testConstants() { test::codemaker::cppumaker::Constants::unsignedHyperMax); } +void Test::testSymbolicConstants() { + CPPUNIT_ASSERT_EQUAL(OUString("byteMin"), test::codemaker::cppumaker::Constants::to_string(static_cast<sal_uInt64>(-128))); + CPPUNIT_ASSERT_EQUAL(OUString("byteMax"), test::codemaker::cppumaker::Constants::to_string(127)); + CPPUNIT_ASSERT_EQUAL(OUString("longMin"), test::codemaker::cppumaker::Constants::to_string(static_cast<sal_uInt64>(-2147483648))); + CPPUNIT_ASSERT_EQUAL(OUString("longMax"), test::codemaker::cppumaker::Constants::to_string(2147483647)); + CPPUNIT_ASSERT_EQUAL(OUString("hyperMin"), test::codemaker::cppumaker::Constants::to_string(static_cast<sal_uInt64>(SAL_MIN_INT64))); + CPPUNIT_ASSERT_EQUAL(OUString("hyperMax"), test::codemaker::cppumaker::Constants::to_string(SAL_MAX_INT64)); + CPPUNIT_ASSERT_EQUAL(OUString("17"), test::codemaker::cppumaker::Constants::to_string(17)); + CPPUNIT_ASSERT_EQUAL(OUString("2147483646"), test::codemaker::cppumaker::Constants::to_string(2147483646)); + + CPPUNIT_ASSERT_EQUAL(OUString("0"), test::codemaker::cppumaker::ByteBits::to_string(0)); + CPPUNIT_ASSERT_EQUAL(OUString("BIT0+BIT2"), test::codemaker::cppumaker::ByteBits::to_string(5)); + CPPUNIT_ASSERT_EQUAL(OUString("BIT4"), test::codemaker::cppumaker::ByteBits::to_string(16)); + CPPUNIT_ASSERT_EQUAL(OUString("BIT0+BIT4"), test::codemaker::cppumaker::ByteBits::to_string(17)); + CPPUNIT_ASSERT_EQUAL(OUString("BIT7"), test::codemaker::cppumaker::ByteBits::to_string(-128)); + CPPUNIT_ASSERT_EQUAL(OUString("ALL"), test::codemaker::cppumaker::ByteBits::to_string(-1)); + + CPPUNIT_ASSERT_EQUAL(OUString("BIT7"), test::codemaker::cppumaker::ShortBits::to_string(128)); + CPPUNIT_ASSERT_EQUAL(OUString("ALL"), test::codemaker::cppumaker::ShortBits::to_string(-1)); + + CPPUNIT_ASSERT_EQUAL(OUString("BIT63"), test::codemaker::cppumaker::UnsignedHyperBits::to_string(9223372036854775808u)); + CPPUNIT_ASSERT_EQUAL(OUString("BIT0+BIT62"), test::codemaker::cppumaker::UnsignedHyperBits::to_string(4611686018427387905)); + CPPUNIT_ASSERT_EQUAL(OUString("BIT0+BIT63"), test::codemaker::cppumaker::UnsignedHyperBits::to_string(9223372036854775809u)); + CPPUNIT_ASSERT_EQUAL(OUString("ALL"), test::codemaker::cppumaker::UnsignedHyperBits::to_string(SAL_MAX_UINT64)); +} + CPPUNIT_TEST_SUITE_REGISTRATION(Test); } diff --git a/cppu/qa/cppumaker/types.idl b/cppu/qa/cppumaker/types.idl index b4dbe2e1de13..25bac18ff920 100644 --- a/cppu/qa/cppumaker/types.idl +++ b/cppu/qa/cppumaker/types.idl @@ -697,6 +697,60 @@ constants Constants { const unsigned hyper unsignedHyperMax = 18446744073709551615; }; +constants ByteBits { + const byte BIT0 = 1; + const byte BIT1 = 2; + const byte BIT2 = 4; + const byte BIT3 = 8; + const byte BIT4 = 16; + const byte BIT5 = 32; + const byte BIT6 = 64; + const byte BIT7 = -128; + const byte ALL = -1; +}; + +constants ShortBits { + const short BIT0 = 1; + const short BIT1 = 2; + const short BIT2 = 4; + const short BIT3 = 8; + const short BIT4 = 16; + const short BIT5 = 32; + const short BIT6 = 64; + const short BIT7 = 128; + const short BIT8 = 256; + const short BIT9 = 512; + const short BIT10 = 1024; + const short BIT11 = 2048; + const short BIT12 = 4096; + const short BIT13 = 8192; + const short BIT14 = 16384; + const short BIT15 = -32768; + const short ALL = -1; +}; + +constants UnsignedHyperBits { + const unsigned hyper BIT0 = 1; + const unsigned hyper BIT1 = 2; + const unsigned hyper BIT2 = 4; + const unsigned hyper BIT3 = 8; + const unsigned hyper BIT4 = 16; + const unsigned hyper BIT5 = 32; + const unsigned hyper BIT6 = 64; + const unsigned hyper BIT7 = 128; + const unsigned hyper BIT8 = 256; + const unsigned hyper BIT9 = 512; + const unsigned hyper BIT10 = 1024; + const unsigned hyper BIT11 = 2048; + const unsigned hyper BIT12 = 4096; + const unsigned hyper BIT13 = 8192; + const unsigned hyper BIT14 = 16384; + const unsigned hyper BIT15 = 32768; + const unsigned hyper BIT62 = 4611686018427387904; + const unsigned hyper BIT63 = 9223372036854775808; + const unsigned hyper ALL = 18446744073709551615; +}; + }; }; }; /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |