/[ascend]/trunk/ascend/compiler/test/test_notes.c
ViewVC logotype

Contents of /trunk/ascend/compiler/test/test_notes.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3397 - (show annotations) (download) (as text)
Tue Jun 12 04:34:02 2018 UTC (7 days, 23 hours ago) by jpye
File MIME type: text/x-csrc
File size: 6707 byte(s)
more notes testing

1 /* ASCEND modelling environment
2 Copyright (C) 2018 Carnegie Mellon University
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *//**
17 @file
18 Unit test functions for compiler. Nothing here yet.
19 */
20 #include <string.h>
21
22 #include <ascend/general/env.h>
23 #include <ascend/general/platform.h>
24 #include <ascend/utilities/ascEnvVar.h>
25 #include <ascend/utilities/error.h>
26
27 #include <ascend/compiler/ascCompiler.h>
28 #include <ascend/compiler/notate.h>
29 #include <ascend/compiler/symtab.h>
30 #include <ascend/compiler/parser.h>
31 #include <ascend/compiler/library.h>
32 #if 0
33 #include <ascend/compiler/module.h>
34 #include <ascend/compiler/simlist.h>
35 #include <ascend/compiler/instquery.h>
36 #include <ascend/compiler/parentchild.h>
37 #include <ascend/compiler/atomvalue.h>
38 #include <ascend/compiler/childio.h>
39 #include <ascend/compiler/initialize.h>
40 #endif
41
42 #include <test/common.h>
43 #include <test/assertimpl.h>
44
45 //#define NOTES_DEBUG
46 #ifdef NOTES_DEBUG
47 # define MSG CONSOLE_DEBUG
48 #else
49 # define MSG(ARGS...) ((void)0)
50 #endif
51
52
53
54 static void test_test1(void){
55 CU_ASSERT(0 == Asc_CompilerInit(0));
56 CU_ASSERT(LibraryNote()==AddSymbol("Loaded Libraries"));
57 //CU_ASSERT(GlobalNote()==AddSymbol("All Known Files"));
58
59 // test that compilerinit created the two expected notes databases
60 struct gl_list_t *l = ListNotesDatabases();
61 CU_ASSERT(gl_length(l)==2);
62 CU_ASSERT(0!=gl_ptr_search(l,(void *)LibraryNote(),0));
63 CU_ASSERT(0!=gl_ptr_search(l,(void *)AddSymbol("All Known Files"),0));
64 gl_destroy(l);
65
66 l = GetNotes(LibraryNote(),NOTESWILD,NOTESWILD,NOTESWILD,NOTESWILD,nd_wild);
67 CU_ASSERT(0==gl_length(l));
68 gl_destroy(l);
69
70 Asc_CompilerDestroy();
71 }
72
73 static const char *model_test2 = "\n\
74 DEFINITION relation\n\
75 included IS_A boolean;\n\
76 message IS_A symbol;\n\
77 included := TRUE;\n\
78 message := 'none';\n\
79 END relation;\n\
80 MODEL test1;\n\
81 NOTES\n\
82 'author' SELF {notesauthor}\n\
83 'description' y {variable called 'y'}\n\
84 'description' x {variable called 'x'}\n\
85 'description' a['left'] {the left one} (*this note won't parse*)\n\
86 END NOTES;\n\
87 x \"hello\" IS_A real;\n\
88 rel1: x - 1 = 0;\n\
89 y[1..5] IS_A real;\n\
90 a['left','right'] IS_A boolean;\n\
91 END test1;\n\
92 MODEL test2;\n\
93 x \"yoohoo\" IS_A real;\n\
94 x^2 - 4 = 0;\n\
95 END test2;\n";
96
97 static void test_test2(void){
98
99 Asc_CompilerInit(1);
100 CU_ASSERT(FindType(AddSymbol("boolean"))!=NULL);
101
102 struct module_t *m;
103 int status;
104
105 m = Asc_OpenStringModule(model_test2, &status, "mystr"/* name prefix*/);
106
107 MSG("Asc_OpenStringModule returns status=%d",status);
108 CU_ASSERT(status==0); /* if successfully created */
109
110 MSG("Beginning parse of %s",Asc_ModuleName(m));
111 status = zz_parse();
112
113 MSG("zz_parse returns status=%d",status);
114 CU_ASSERT(status==0);
115
116 struct gl_list_t *l = Asc_TypeByModule(m);
117 struct gl_list_t *l2;
118 MSG("%lu library entries loaded from %s",gl_length(l),Asc_ModuleName(m));
119 CU_ASSERT(gl_length(l)==3);
120 gl_destroy(l);
121
122 CU_ASSERT(FindType(AddSymbol("test1"))!=NULL);
123
124 l = GetNotes(LibraryNote(),NOTESWILD,NOTESWILD,NOTESWILD,NOTESWILD,nd_wild);
125 CU_ASSERT(gl_length(l)==5);
126 int i;
127 for(i=1;i<=gl_length(l);++i){
128 struct Note *N = gl_fetch(l,i);
129 l2 = GetExactNote(LibraryNote(),N);
130 CU_ASSERT_FATAL(l2 != NULL);
131 CU_ASSERT(gl_length(l2)==1);
132 CU_ASSERT(gl_fetch(l2,1)==N);
133 gl_destroy(l2);
134 MSG("%s:%d (#%d) id='%s', type='%s', lang='%s', meth='%s': text='%s'"
135 ,GetNoteFilename(N),GetNoteLineNum(N),i,SCP(GetNoteId(N))
136 ,SCP(GetNoteType(N)),SCP(GetNoteLanguage(N)),SCP(GetNoteMethod(N))
137 ,BCS(GetNoteText(N))
138 );
139 }
140 gl_destroy(l);
141
142 // GetNotes(dbid,typename,language,id,method,notedata);
143 l = GetNotes(LibraryNote(),AddSymbol("test1"),AddSymbol("inline"),AddSymbol("x"),NOTESWILD,nd_wild);
144 CU_ASSERT(gl_length(l)==1);
145 CU_ASSERT(0==strcmp(BCS(GetNoteText(gl_fetch(l,1))),"hello"));
146 CU_ASSERT(AddSymbol("inline")==GetNoteLanguage(gl_fetch(l,1)));
147 CU_ASSERT(0==strcmp("mystr_global_1<0>",GetNoteFilename(gl_fetch(l,1))));
148 CU_ASSERT(15==GetNoteLineNum(gl_fetch(l,1)));
149 CU_ASSERT(NULL==GetNoteMethod(gl_fetch(l,1)));
150 CU_ASSERT(AddSymbol("test1")==GetNoteType(gl_fetch(l,1)));
151 gl_destroy(l);
152
153 // HoldNoteData, HeldNotes, ReleaseNoteData
154 void *h1, *h2;
155 // clear all held notes
156 ReleaseNoteData(LibraryNote(),(void*)0x1);
157 // TODO attempt to store a bogus list
158 l2 = gl_create(10);
159 char *xx = "nothing";
160 gl_append_ptr(l2,xx);
161 h1 = HoldNoteData(LibraryNote(),l2);
162 CU_ASSERT(h1 == NULL);
163 gl_destroy(l2);
164 // store some real notes
165 l = GetNotes(LibraryNote(),NOTESWILD,AddSymbol("inline"),NOTESWILD,NOTESWILD,nd_wild);
166 CU_ASSERT(gl_length(l)==2);
167 h1 = HoldNoteData(LibraryNote(),l);
168 CU_ASSERT(h1 != NULL);
169 // retreive stored notes, check they are as expected
170 l2 = HeldNotes(LibraryNote(),h1);
171 CU_ASSERT(l2 != NULL);
172 CU_ASSERT(l == l2);
173 CU_ASSERT(0==strcmp(BCS(GetNoteText(gl_fetch(l2,1))),"yoohoo"));
174 CU_ASSERT(0==strcmp(BCS(GetNoteText(gl_fetch(l2,2))),"hello"));
175 CU_ASSERT(2==gl_length(l2));
176 ReleaseNoteData(LibraryNote(),h1);
177 //gl_destroy(l);
178
179 // release all held lists (shouldn't be any others there anyway
180 ReleaseNoteData(LibraryNote(),(void *)0x1);
181
182 l = GetNotes(LibraryNote(),NOTESWILD,AddSymbol("author"),NOTESWILD,NOTESWILD,nd_wild);
183 CU_ASSERT(gl_length(l)==1);
184 h1 = HoldNoteData(LibraryNote(),l);
185 CU_ASSERT(h1 !=NULL);
186
187 l2 = GetNotes(LibraryNote(),NOTESWILD,AddSymbol("description"),NOTESWILD,NOTESWILD,nd_wild);
188 CU_ASSERT(gl_length(l2)==2);
189 h2 = HoldNoteData(LibraryNote(),l2);
190 CU_ASSERT(h2 !=NULL);
191
192 // release all held lists (shouldn't be any others there anyway
193 ReleaseNoteData(LibraryNote(),(void *)0x1);
194
195 DestroyNotesOnType(LibraryNote(),AddSymbol("test1"));
196 l = GetNotes(LibraryNote(),NOTESWILD,NOTESWILD,NOTESWILD,NOTESWILD,nd_wild);
197 CU_ASSERT(gl_length(l)==1);
198 gl_destroy(l);
199 DestroyNotesOnType(LibraryNote(),AddSymbol("test2"));
200 l = GetNotes(LibraryNote(),NOTESWILD,NOTESWILD,NOTESWILD,NOTESWILD,nd_wild);
201 CU_ASSERT(gl_length(l)==0);
202 gl_destroy(l);
203
204 Asc_CompilerDestroy();
205 }
206
207 /*===========================================================================*/
208 /* Registration information */
209
210 #define TESTS(T) \
211 T(test1) \
212 T(test2)
213
214 REGISTER_TESTS_SIMPLE(compiler_notes, TESTS)

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22