GlueGen v2.6.0-rc-20250712
GlueGen, Native Binding Generator for Java™ (public API).
TestJCPP.java
Go to the documentation of this file.
1/**
2 * Copyright 2010-2023 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28
29package com.jogamp.gluegen.test.junit.generation;
30
31import com.jogamp.common.os.AndroidVersion;
32import com.jogamp.gluegen.jcpp.JCPP;
33import com.jogamp.gluegen.jcpp.LexerException;
34import com.jogamp.junit.util.SingletonJunitCase;
35
36import java.io.BufferedReader;
37import java.io.ByteArrayOutputStream;
38import java.io.FileNotFoundException;
39import java.io.FileReader;
40import java.io.IOException;
41import java.util.Collections;
42
43import org.junit.BeforeClass;
44import org.junit.Test;
45import static org.junit.Assert.*;
46
47/**
48 * serves mainly as entry point for debugging purposes.
49 * @author Sven Gothel, Michael Bien
50 */
51import org.junit.FixMethodOrder;
52import org.junit.runners.MethodSorters;
53
54@FixMethodOrder(MethodSorters.NAME_ASCENDING)
55public class TestJCPP extends SingletonJunitCase {
56
57 @BeforeClass
58 public static void init() {
60 // JCPP is n/a on Android - GlueGen Runtime only
61 setTestSupported(false);
62 }
63 }
64
65 @Test
67 Exception ex = null;
68 try {
69 testMacroAndInc(false);
70 } catch (IOException | LexerException e) {
71 e.printStackTrace();
72 ex = e;
73 }
74 assertNull(ex);
75 }
76
77 @Test
79 Exception ex = null;
80 try {
81 testMacroAndInc(true);
82 } catch (IOException | LexerException e) {
83 e.printStackTrace();
84 ex = e;
85 }
86 assertNull(ex);
87 }
88
89 public void testMacroAndInc(final boolean pragmaOnce) throws FileNotFoundException, IOException, LexerException {
90 final String folderpath = BuildEnvironment.gluegenRoot + "/src/junit/com/jogamp/gluegen/test/junit/generation";
91 final JCPP pp = new JCPP(Collections.<String>singletonList(folderpath), false, false, pragmaOnce);
92 if( pragmaOnce ) {
93 pp.addDefine("PRAGMA_ONCE_ENABLED", "1");
94 }
95 final ByteArrayOutputStream output = new ByteArrayOutputStream();
96 pp.setOut(output);
97
98 final String filename = "cpptest_1.h";
99 final String filepath = folderpath + "/" + filename ;
100 pp.run(new BufferedReader(new FileReader(filepath)), filename);
101
102 final String expected =
103 "#line 1 \"cpptest_1.h\" 1"+
104 ""+
105 "typedef char cl_char;"+
106 "cl_char GOOD_A;"+
107 "int GOOD_B;"+
108 "int GOOD_C;"+
109 ""+
110 " int TEST_D_GOOD;"+
111 ""+
112 "/***"+
113 " ** STD API file .."+
114 " */"+
115 ""+
116 "int GOOD_F_1;"+
117 "int GOOD_F_2;"+
118 ""+
119 "int GOOD_G;"+
120 ""+
121 "#line 1 \""+folderpath+"/cpptest-included.h\" 1"+
122 ""+
123 ( pragmaOnce ?
124 " const int pragma_once_enabled = 1;"
125 : " const int pragma_once_enabled = 0;"
126 )+
127 ""+
128 " // pragma-once or macro-defined test, i.e. should not be included recursively"+
129 "#line 1 \""+folderpath+"/cpptest-included.h\" 1"+
130 ""+
131 "#line 13 \""+folderpath+"/cpptest-included.h\" 2"+
132 ""+
133 "const int GOOD_H = 42;"+
134 ""+
135 "#line 136 \"cpptest_1.h\" 2"+
136 "#line 1 \""+folderpath+"/sub-inc/-cpptest-included2.h\" 1"+
137 ""+
138 "const int GOOD_I = 43;"+
139 "#line 137 \"cpptest_1.h\" 2"+
140 ""+
141 "typedef enum SomeEnum {"+
142 " ConstEnumValue00 = 16,"+
143 " ConstEnumValue01 = (1 << ConstEnumValue00) - 1,"+
144 " ConstEnumValue02 = (10-1),"+
145 " ConstEnumValue03 = (10 - 2),"+
146 " ConstEnumValue04 = ( 10 - 3 ),"+
147 " ConstEnumValue05 = 10-4,"+
148 " ConstEnumValue06 = 10 - 11,"+
149 " ConstEnumValue07 = -2,"+
150 " ConstEnumValue08 = - 2,"+
151 " ConstEnumValueXX = 0"+
152 "} SomeEnum;"+
153 ""+
154 "const int constInt00 = 16;"+
155 "const int constInt01 = ((1 << 16) - 1);"+
156 "const int constInt02 = (10-1);"+
157 "const int constInt03 = (10 - 2);"+
158 "const int constInt04 = ( 10 - 3 );"+
159 "const int constInt05 = 10-4;"+
160 "const int constInt06 = 10 - 11;"+
161 "const int constInt07 = -2;"+
162 "const int constInt08 = - 2;"+
163 "const int constIntXX = 0;"+
164 ""
165 ;
166
167 output.flush();
168 final String result = output.toString();
169 output.close();
170
171 System.err.println("Expected: ");
172 System.err.println("-------------------------------");
173 System.err.println(killWhitespace(expected));
174 System.err.println("-------------------------------");
175 System.err.println();
176 System.err.println("Result: ");
177 System.err.println("-------------------------------");
178 // System.err.println(result);
179 System.err.println(killWhitespace(result));
180 System.err.println("-------------------------------");
181 System.err.println();
182
183 assertEquals(killWhitespace(expected), killWhitespace(result));
184 }
185
186 private String killWhitespace(final String a) {
187 return a.replaceAll("\\p{javaWhitespace}+", "");
188 }
189
190 public static void main(final String args[]) throws IOException {
191 final String tstname = TestJCPP.class.getName();
192 org.junit.runner.JUnitCore.main(tstname);
193 }
194}
static void main(final String args[])
Definition: TestJCPP.java:190
void testMacroAndInc(final boolean pragmaOnce)
Definition: TestJCPP.java:89