|
| 1 | +/* |
| 2 | + * Copyright 2013-2024 Real Logic Limited. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package uk.co.real_logic.sbe.generation.cpp; |
| 17 | + |
| 18 | +import org.agrona.generation.StringWriterOutputManager; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | +import uk.co.real_logic.sbe.Tests; |
| 21 | +import uk.co.real_logic.sbe.ir.Ir; |
| 22 | +import uk.co.real_logic.sbe.xml.IrGenerator; |
| 23 | +import uk.co.real_logic.sbe.xml.MessageSchema; |
| 24 | +import uk.co.real_logic.sbe.xml.ParserOptions; |
| 25 | + |
| 26 | +import java.io.InputStream; |
| 27 | + |
| 28 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 29 | +import static org.hamcrest.Matchers.containsString; |
| 30 | +import static org.hamcrest.Matchers.not; |
| 31 | +import static uk.co.real_logic.sbe.xml.XmlSchemaParser.parse; |
| 32 | + |
| 33 | +class CppGeneratorTest |
| 34 | +{ |
| 35 | + @Test |
| 36 | + void shouldUseGeneratedLiteralForConstantOneWhenGeneratingBitsetCode() throws Exception |
| 37 | + { |
| 38 | + try (InputStream in = Tests.getLocalResource("issue827.xml")) |
| 39 | + { |
| 40 | + final ParserOptions options = ParserOptions.builder().stopOnError(true).build(); |
| 41 | + final MessageSchema schema = parse(in, options); |
| 42 | + final IrGenerator irg = new IrGenerator(); |
| 43 | + final Ir ir = irg.generate(schema); |
| 44 | + final StringWriterOutputManager outputManager = new StringWriterOutputManager(); |
| 45 | + outputManager.setPackageName(ir.applicableNamespace()); |
| 46 | + |
| 47 | + final CppGenerator generator = new CppGenerator(ir, false, outputManager); |
| 48 | + generator.generate(); |
| 49 | + |
| 50 | + final String source = outputManager.getSource("issue827.FlagsSet").toString(); |
| 51 | + assertThat(source, not(containsString("1u << "))); |
| 52 | + assertThat(source, containsString("UINT64_C(0x1) << ")); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void shouldUseConstexprWhenInitializingSemanticVersion() throws Exception |
| 58 | + { |
| 59 | + try (InputStream in = Tests.getLocalResource("code-generation-schema.xml")) |
| 60 | + { |
| 61 | + final ParserOptions options = ParserOptions.builder().stopOnError(true).build(); |
| 62 | + final MessageSchema schema = parse(in, options); |
| 63 | + final IrGenerator irg = new IrGenerator(); |
| 64 | + final Ir ir = irg.generate(schema); |
| 65 | + final StringWriterOutputManager outputManager = new StringWriterOutputManager(); |
| 66 | + outputManager.setPackageName(ir.applicableNamespace()); |
| 67 | + |
| 68 | + final CppGenerator generator = new CppGenerator(ir, false, outputManager); |
| 69 | + generator.generate(); |
| 70 | + |
| 71 | + final String source = outputManager.getSource("code.generation.test.Car").toString(); |
| 72 | + assertThat(source, containsString("static constexpr const char* SBE_SEMANTIC_VERSION = \"5.2\"")); |
| 73 | + } |
| 74 | + } |
| 75 | +} |
0 commit comments