diff options
author | Josh Haberman <jhaberman@gmail.com> | 2016-04-06 14:28:45 -0700 |
---|---|---|
committer | Josh Haberman <jhaberman@gmail.com> | 2016-04-06 14:28:45 -0700 |
commit | f8c26358f0b87a9c90be3f038cdea5bdcae185a0 (patch) | |
tree | a51e3b278d368ebd4fc01b0fff68c8f3f0701299 /tests | |
parent | e9d79d2441732264e2b990a5b2dc76d13724db07 (diff) |
Changes for PR comments.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/pb/test_encoder.cc | 21 | ||||
-rw-r--r-- | tests/test_def.c | 21 | ||||
-rw-r--r-- | tests/test_util.h | 99 |
3 files changed, 65 insertions, 76 deletions
diff --git a/tests/pb/test_encoder.cc b/tests/pb/test_encoder.cc index 6dd10ba..6219e08 100644 --- a/tests/pb/test_encoder.cc +++ b/tests/pb/test_encoder.cc @@ -1,4 +1,5 @@ +#include "tests/test_util.h" #include "tests/upb_test.h" #include "upb/bindings/stdc++/string.h" #include "upb/descriptor/descriptor.upbdefs.h" @@ -6,26 +7,6 @@ #include "upb/pb/encoder.h" #include "upb/pb/glue.h" -static char *upb_readfile(const char *filename, size_t *len) { - long size; - char *buf; - FILE *f = fopen(filename, "rb"); - if(!f) return NULL; - if(fseek(f, 0, SEEK_END) != 0) goto error; - size = ftell(f); - if(size < 0) goto error; - if(fseek(f, 0, SEEK_SET) != 0) goto error; - buf = (char*)malloc(size + 1); - if(size && fread(buf, size, 1, f) != 1) goto error; - fclose(f); - if (len) *len = size; - return buf; - -error: - fclose(f); - return NULL; -} - std::string read_string(const char *filename) { size_t len; char *str = upb_readfile(filename, &len); diff --git a/tests/test_def.c b/tests/test_def.c index 6f3fb8f..cc766e0 100644 --- a/tests/test_def.c +++ b/tests/test_def.c @@ -3,6 +3,7 @@ ** (like attempts to link defs that don't have required properties set). */ +#include "tests/test_util.h" #include "upb/def.h" #include "upb/pb/glue.h" #include "upb_test.h" @@ -38,26 +39,6 @@ static void test_noreftracking() { upb_msgdef_unref(md, &md); } -static char *upb_readfile(const char *filename, size_t *len) { - long size; - char *buf; - FILE *f = fopen(filename, "rb"); - if(!f) return NULL; - if(fseek(f, 0, SEEK_END) != 0) goto error; - size = ftell(f); - if(size < 0) goto error; - if(fseek(f, 0, SEEK_SET) != 0) goto error; - buf = malloc(size + 1); - if(size && fread(buf, size, 1, f) != 1) goto error; - fclose(f); - if (len) *len = size; - return buf; - -error: - fclose(f); - return NULL; -} - static upb_symtab *load_test_proto(void *owner) { upb_symtab *s = upb_symtab_new(owner); upb_status status = UPB_STATUS_INIT; diff --git a/tests/test_util.h b/tests/test_util.h index caf9ee8..c6438fc 100644 --- a/tests/test_util.h +++ b/tests/test_util.h @@ -11,23 +11,26 @@ #include "upb/env.h" #include "upb/sink.h" +#ifdef __cplusplus + upb::BufferHandle global_handle; -// A convenience class for parser tests. Provides some useful features: -// -// - can support multiple calls to parse, to test the parser's handling -// of buffer seams. -// -// - can output verbose output about each parse call when requested, for -// ease of debugging. -// -// - can pass NULL for skipped regions of the input if requested. -// -// - allocates and passes a separate buffer for each parsed region, to -// ensure that the parser is not erroneously overreading its buffer. +/* A convenience class for parser tests. Provides some useful features: + * + * - can support multiple calls to parse, to test the parser's handling + * of buffer seams. + * + * - can output verbose output about each parse call when requested, for + * ease of debugging. + * + * - can pass NULL for skipped regions of the input if requested. + * + * - allocates and passes a separate buffer for each parsed region, to + * ensure that the parser is not erroneously overreading its buffer. + */ class VerboseParserEnvironment { public: - // Pass verbose=true to print detailed diagnostics to stderr. + /* Pass verbose=true to print detailed diagnostics to stderr. */ VerboseParserEnvironment(bool verbose) : verbose_(verbose) { env_.SetErrorFunction(&VerboseParserEnvironment::OnError, this); } @@ -60,15 +63,16 @@ class VerboseParserEnvironment { skipped_with_null_ = false; } - // The user should call a series of: - // - // Reset(buf, len, may_skip); - // Start() - // ParseBuffer(X); - // ParseBuffer(Y); - // // Repeat ParseBuffer as desired, but last call should pass -1. - // ParseBuffer(-1); - // End(); + /* The user should call a series of: + * + * Reset(buf, len, may_skip); + * Start() + * ParseBuffer(X); + * ParseBuffer(Y); + * // Repeat ParseBuffer as desired, but last call should pass -1. + * ParseBuffer(-1); + * End(); + */ bool Start() { @@ -111,9 +115,9 @@ class VerboseParserEnvironment { ASSERT((size_t)bytes <= (len_ - ofs_)); - // Copy buffer into a separate, temporary buffer. - // This is necessary to verify that the parser is not erroneously - // reading outside the specified bounds. + /* Copy buffer into a separate, temporary buffer. + * This is necessary to verify that the parser is not erroneously + * reading outside the specified bounds. */ char *buf2 = NULL; if ((int)(ofs_ + bytes) <= skip_until_) { @@ -125,7 +129,7 @@ class VerboseParserEnvironment { } if (buf2 == NULL && bytes == 0) { - // Decoders dont' support buf=NULL, bytes=0. + /* Decoders dont' support buf=NULL, bytes=0. */ return true; } @@ -189,18 +193,41 @@ class VerboseParserEnvironment { bool end_ok_; bool end_ok_set_; - // When our parse call returns a value greater than the number of bytes - // we passed in, the decoder is indicating to us that the next N bytes - // in the stream are not needed and can be skipped. The user is allowed - // to pass a NULL buffer for those N bytes. - // - // skip_until_ is initially set to 0 if we should do this NULL-buffer - // skipping or -1 if we should not. If we are open to doing NULL-buffer - // skipping and we get an opportunity to do it, we set skip_until to the - // stream offset where we can skip until. The user can then test whether - // this happened by testing SkippedWithNull(). + /* When our parse call returns a value greater than the number of bytes + * we passed in, the decoder is indicating to us that the next N bytes + * in the stream are not needed and can be skipped. The user is allowed + * to pass a NULL buffer for those N bytes. + * + * skip_until_ is initially set to 0 if we should do this NULL-buffer + * skipping or -1 if we should not. If we are open to doing NULL-buffer + * skipping and we get an opportunity to do it, we set skip_until to the + * stream offset where we can skip until. The user can then test whether + * this happened by testing SkippedWithNull(). */ int skip_until_; bool skipped_with_null_; }; +#endif /* __cplusplus */ + +UPB_INLINE char *upb_readfile(const char *filename, size_t *len) { + long size; + char *buf; + FILE *f = fopen(filename, "rb"); + if(!f) return NULL; + if(fseek(f, 0, SEEK_END) != 0) goto error; + size = ftell(f); + if(size < 0) goto error; + if(fseek(f, 0, SEEK_SET) != 0) goto error; + buf = (char*)malloc(size + 1); + if(size && fread(buf, size, 1, f) != 1) goto error; + fclose(f); + if (len) *len = size; + buf[size] = '\0'; + return buf; + +error: + fclose(f); + return NULL; +} + #endif |