Skip to content

Commit 64c8f7b

Browse files
committed
wip
1 parent cae90e8 commit 64c8f7b

File tree

3 files changed

+168
-41
lines changed

3 files changed

+168
-41
lines changed

rapidyaml/native/org_rapidyaml_Rapidyaml.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,9 @@ RYML_EXPORT void ysparse_timing_set(bool yes)
154154

155155
static C4_NO_INLINE void throw_java_exception(JNIEnv * env, const char* type, const char* msg)
156156
{
157-
jclass clazz = env->FindClass(type);
158-
if (clazz != NULL) // if it is null, a NoClassDefFoundError was already thrown
159-
env->ThrowNew(clazz, msg);
157+
jclass cls = env->FindClass(type);
158+
if (cls != NULL) // if it is null, a NoClassDefFoundError was already thrown
159+
env->ThrowNew(cls, msg);
160160
}
161161

162162
static C4_NO_INLINE void throw_runtime_exception(JNIEnv *env, const char* msg)
@@ -167,8 +167,8 @@ static C4_NO_INLINE void throw_runtime_exception(JNIEnv *env, const char* msg)
167167
static C4_NO_INLINE void throw_parse_error(JNIEnv *env, size_t offset, size_t line, size_t column, const char *msg)
168168
{
169169
// see https://stackoverflow.com/questions/55013243/jni-custom-exceptions-with-more-than-one-parameter
170-
jclass clazz = env->FindClass("org/rapidyaml/YamlParseErrorException");
171-
if (clazz != NULL) // if it is null, a NoClassDefFoundError was already thrown
170+
jclass cls = env->FindClass("org/rapidyaml/YamlParseErrorException");
171+
if (cls != NULL) // if it is null, a NoClassDefFoundError was already thrown
172172
{
173173
jstring jmsg = env->NewStringUTF(msg);
174174
jint joffset = (jint)offset;
@@ -178,8 +178,8 @@ static C4_NO_INLINE void throw_parse_error(JNIEnv *env, size_t offset, size_t li
178178
// about the proper signature.
179179
// we want <init>(int, int, int, String):
180180
const char * const signature = "(IIILjava/lang/String;)V";
181-
jmethodID ctor = env->GetMethodID(clazz, "<init>", signature);
182-
jobject jexc = env->NewObject(clazz, ctor, joffset, jline, jcol, jmsg);
181+
jmethodID ctor = env->GetMethodID(cls, "<init>", signature);
182+
jobject jexc = env->NewObject(cls, ctor, joffset, jline, jcol, jmsg);
183183
env->Throw((jthrowable)jexc); // https://stackoverflow.com/questions/2455668/jni-cast-between-jobect-and-jthrowable
184184
}
185185
}

rapidyaml/native/ysparse_test.cpp

Lines changed: 136 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
using c4::csubstr;
66
using c4::substr;
7+
using ryml::Location;
8+
79

810
namespace c4
911
{
@@ -94,39 +96,54 @@ size_t expected_size(std::vector<EvtWithScalar> const& evt)
9496
return exp;
9597
}
9698

99+
#define _runtest(name, ...) \
100+
do { \
101+
printf("[ RUN ] %s ... \n", #name); \
102+
TestResult tr_ = name(__VA_ARGS__); \
103+
tr.add(tr_); \
104+
printf("[ %s ] %s\n", tr_?"OK ":"FAIL", #name); \
105+
} while(0)
106+
107+
#define CHECK(cond) \
108+
do { \
109+
bool pass = !!(cond); \
110+
++tr.num_assertions; \
111+
if(!pass) { \
112+
printf("%s:%d: fail! %s\n", __FILE__, __LINE__, #cond); \
113+
++tr.num_failed_assertions; \
114+
} \
115+
} while(0)
116+
117+
#define CHECK_EQ(lhs, rhs) \
118+
do { \
119+
bool pass = !!(lhs == rhs); \
120+
++tr.num_assertions; \
121+
if(!pass) { \
122+
std::string slhs = c4::catrs<std::string>(lhs); \
123+
std::string srhs = c4::catrs<std::string>(rhs); \
124+
printf("%s:%d: fail! %s=%s == %s=%s\n", __FILE__, __LINE__, #lhs, slhs.c_str(), #rhs, srhs.c_str()); \
125+
++tr.num_failed_assertions; \
126+
} \
127+
} while(0)
128+
129+
#define CHECK_MSG(cond, fmt, ...) \
130+
do { \
131+
bool pass = !!(cond); \
132+
++tr.num_assertions; \
133+
if(!pass) { \
134+
printf("%s:%d: fail! %s:" fmt "\n", __FILE__, __LINE__, #cond, ## __VA_ARGS__); \
135+
++tr.num_failed_assertions; \
136+
} \
137+
} while(0)
138+
139+
97140
struct TestCase
98141
{
99142
csubstr ys;
100143
std::vector<EvtWithScalar> evt;
101144

102145
public:
103146

104-
#define _runtest(name, ...) \
105-
do { \
106-
printf("[ RUN ] %s ... \n", #name); \
107-
TestResult tr_ = name(__VA_ARGS__); \
108-
tr.add(tr_); \
109-
printf("[ %s ] %s\n", tr_?"OK ":"FAIL", #name); \
110-
} while(0)
111-
#define CHECK(cond) \
112-
do { \
113-
bool pass = !!(cond); \
114-
++tr.num_assertions; \
115-
if(!pass) { \
116-
printf("%s:%d: fail! %s\n", __FILE__, __LINE__, #cond); \
117-
++tr.num_failed_assertions; \
118-
} \
119-
} while(0)
120-
#define CHECK_MSG(cond, fmt, ...) \
121-
do { \
122-
bool pass = !!(cond); \
123-
++tr.num_assertions; \
124-
if(!pass) { \
125-
printf("%s:%d: fail! %s:" fmt "\n", __FILE__, __LINE__, #cond, ## __VA_ARGS__); \
126-
++tr.num_failed_assertions; \
127-
} \
128-
} while(0)
129-
130147
TestResult test(ysparse *ryml2evt) const
131148
{
132149
TestResult tr = {};
@@ -298,6 +315,65 @@ struct TestCase
298315
};
299316

300317

318+
//-----------------------------------------------------------------------------
319+
320+
struct TestCaseErr
321+
{
322+
csubstr ys;
323+
bool is_parse_err;
324+
ryml::Location loc;
325+
326+
TestCaseErr(csubstr ys_) : ys(ys_), is_parse_err(false), loc() {}
327+
TestCaseErr(csubstr ys_, ryml::Location loc_) : ys(ys_), is_parse_err(true), loc(loc_) {}
328+
329+
TestResult test(ysparse *ryml2evt) const
330+
{
331+
TestResult tr = {};
332+
_runtest(test_err, );
333+
_runtest(test_err_reuse, ryml2evt);
334+
return tr;
335+
}
336+
337+
TestResult test_err_reuse(ysparse *ryml2evt) const
338+
{
339+
TestResult tr = {};
340+
std::string input_(ys.begin(), ys.end());
341+
substr input = c4::to_substr(input_);
342+
bool gotit = false;
343+
try
344+
{
345+
size_type reqsize = ysparse_parse(ryml2evt, "ysfilename",
346+
input.str, (size_type)input.len,
347+
nullptr, 0);
348+
}
349+
catch(YsParseError const& exc)
350+
{
351+
if(is_parse_err)
352+
{
353+
gotit = true;
354+
CHECK_EQ(exc.location.name, "ysfilename");
355+
CHECK_EQ(exc.location.line, loc.line);
356+
CHECK_EQ(exc.location.col, loc.col);
357+
CHECK_EQ(exc.location.offset, loc.offset);
358+
}
359+
}
360+
catch(std::exception const& exc)
361+
{
362+
if(!is_parse_err)
363+
gotit = true;
364+
}
365+
CHECK(gotit);
366+
return tr;
367+
return tr;
368+
}
369+
TestResult test_err() const
370+
{
371+
Ys2EvtScoped lib;
372+
return test_err_reuse(lib.ryml2evt);
373+
}
374+
};
375+
376+
301377
//-----------------------------------------------------------------------------
302378

303379
namespace {
@@ -308,6 +384,20 @@ using namespace evt;
308384
inline constexpr bool needs_filter = true;
309385
const TestCase test_cases[] = {
310386
// case -------------------------------------------------
387+
tc("!yamlscript/v0/bare\n--- !code\n42\n",
388+
{
389+
e(BSTR),
390+
e(BDOC),
391+
e(VAL_|TAG_, 1, 18, "yamlscript/v0/bare"),
392+
e(VAL_|SCLR|PLAI, 0, 0, ""),
393+
e(EDOC),
394+
e(BDOC|EXPL),
395+
e(VAL_|TAG_, 25, 4, "code"),
396+
e(VAL_|SCLR|PLAI, 30, 2, "42"),
397+
e(EDOC),
398+
e(ESTR),
399+
}),
400+
// case -------------------------------------------------
311401
tc("a: 1",
312402
{
313403
e(BSTR),
@@ -568,8 +658,16 @@ defn run(prompt session=nil):
568658
e(ESTR),
569659
}),
570660
};
661+
#define tcf(...) TestCaseErr(__VA_ARGS__)
662+
const TestCaseErr test_cases_err[] = {
663+
tcf("- !!str, xxx\n", Location(13, 2, 1)),
664+
//FIXME tcf(": : : :", Location(2, 1, 3)),
665+
};
571666
} // namespace
572667

668+
669+
//-----------------------------------------------------------------------------
670+
573671
int main(int argc, const char *argv[])
574672
{
575673
for(int i = 1; i < argc; ++i)
@@ -592,9 +690,20 @@ int main(int argc, const char *argv[])
592690
failed_cases += (!tr);
593691
printf("case %zu/%zu: %s\n", i, C4_COUNTOF(test_cases), tr ? "ok!" : "failed");
594692
}
693+
size_t num_cases_err = C4_COUNTOF(test_cases_err);
694+
for(size_t i = 0; i < C4_COUNTOF(test_cases_err); ++i)
695+
{
696+
printf("-----------------------------------------\n"
697+
"errcase %zu/%zu ...\n"
698+
"[%zu]~~~%.*s~~~\n", i, num_cases_err, test_cases_err[i].ys.len, (int)test_cases_err[i].ys.len, test_cases_err[i].ys.str);
699+
const TestResult tr = test_cases_err[i].test(ys2evt.ryml2evt);
700+
total.add(tr);
701+
failed_cases += (!tr);
702+
printf("case %zu/%zu: %s\n", i, C4_COUNTOF(test_cases), tr ? "ok!" : "failed");
703+
}
595704
printf("assertions: %u/%u pass %u/%u fail\n", total.num_assertions - total.num_failed_assertions, total.num_assertions, total.num_failed_assertions, total.num_assertions);
596705
printf("tests: %u/%u pass %u/%u fail\n", total.num_tests - total.num_failed_tests, total.num_tests, total.num_failed_tests, total.num_tests);
597-
printf("cases: %zu/%zu pass %zu/%zu fail\n", num_cases-failed_cases, num_cases, failed_cases, num_cases);
706+
printf("cases: %zu/%zu pass %zu/%zu fail\n", num_cases-failed_cases, num_cases+num_cases_err, failed_cases, num_cases);
598707
if(total)
599708
printf("TESTS SUCCEED!\n");
600709
return total ? 0 : -1;

rapidyaml/src/test/java/org/rapidyaml/RapidyamlTest.java

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,24 @@ public void testTaggedSeq()
101101
testEvt_(ys, expected);
102102
}
103103

104+
public void testDocTag()
105+
{
106+
String ys = "!yamlscript/v0/bare\n--- !code\n42\n";
107+
ExpectedEvent[] expected = {
108+
mkev(Evt.BSTR),
109+
mkev(Evt.BDOC),
110+
mkev(Evt.VAL_|Evt.TAG_, 1, 18, "yamlscript/v0/bare"),
111+
mkev(Evt.VAL_|Evt.SCLR|Evt.PLAI, 0, 0, ""),
112+
mkev(Evt.EDOC),
113+
mkev(Evt.BDOC|Evt.EXPL),
114+
mkev(Evt.VAL_|Evt.TAG_, 25, 4, "code"),
115+
mkev(Evt.VAL_|Evt.SCLR|Evt.PLAI, 30, 2, "42"),
116+
mkev(Evt.EDOC),
117+
mkev(Evt.ESTR),
118+
};
119+
testEvt_(ys, expected);
120+
}
121+
104122
public void testLargeCase()
105123
{
106124
String ys = "--- !yamlscript/v0\n" +
@@ -206,7 +224,7 @@ public void testFilterCase()
206224
public void testFailure() throws Exception
207225
{
208226
Rapidyaml rapidyaml = new Rapidyaml();
209-
String ys = ": : : :";
227+
String ys = "{a: b";
210228
byte[] src = ys.getBytes(StandardCharsets.UTF_8);
211229
byte[] srcbuf = new byte[src.length];
212230
boolean gotit = false;
@@ -215,9 +233,9 @@ public void testFailure() throws Exception
215233
}
216234
catch(YamlParseErrorException e) {
217235
gotit = true;
218-
assertEquals(2, e.offset);
236+
assertEquals(5, e.offset);
219237
assertEquals(1, e.line);
220-
assertEquals(3, e.column);
238+
assertEquals(6, e.column);
221239
assertTrue(e.getMessage() != null);
222240
assertFalse(e.getMessage().isEmpty());
223241
}
@@ -233,7 +251,7 @@ public void testFailure() throws Exception
233251
public void testFailureBuf() throws Exception
234252
{
235253
Rapidyaml rapidyaml = new Rapidyaml();
236-
String ys = ": : : :";
254+
String ys = "{a: b";
237255
byte[] src = ys.getBytes(StandardCharsets.UTF_8);
238256
ByteBuffer bbuf = ByteBuffer.allocateDirect(src.length);
239257
bbuf.put(src);
@@ -243,9 +261,9 @@ public void testFailureBuf() throws Exception
243261
}
244262
catch(YamlParseErrorException e) {
245263
gotit = true;
246-
assertEquals(2, e.offset);
264+
assertEquals(5, e.offset);
247265
assertEquals(1, e.line);
248-
assertEquals(3, e.column);
266+
assertEquals(6, e.column);
249267
assertTrue(e.getMessage() != null);
250268
assertFalse(e.getMessage().isEmpty());
251269
}
@@ -300,7 +318,7 @@ private void testEvt_(String ys, ExpectedEvent[] expected)
300318
}
301319
}
302320

303-
boolean dbglog = true;
321+
boolean dbglog = false;
304322
private void cmpEvt_(String ys, byte[] src, int[] actual, ExpectedEvent[] expected)
305323
{
306324
if(dbglog) {

0 commit comments

Comments
 (0)