<commit_before>/*
 * Copyright 2016 MongoDB, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


#include <bson.h>

#include "TestSuite.h"
#include "json-test.h"

#include <limits.h>

#ifdef _MSC_VER
#define PATH_MAX 1024
#define realpath(path, expanded) GetFullPathName (path, PATH_MAX, expanded, NULL)
#endif

typedef void (*test_bson_type_valid_cb)(const bson_t *input, const char *expected, const bson_t *json, bool roundtrip_json);

#ifdef _MSC_VER
# define SSCANF sscanf_s
#else
# define SSCANF sscanf
#endif

#ifndef JSON_DIR
# define JSON_DIR "tests/json"
#endif

void
test_bson_type_int32 (const bson_t *input,
                      const char   *expected,
                      const bson_t *json,
                      bool          roundtrip_json)
{
   int32_t number1, number2;
   bson_t *bson;
   bson_iter_t iter;
   char *str1, *str2;

   BSON_ASSERT (input);
   BSON_ASSERT (expected);
   BSON_ASSERT (json);


   SSCANF (expected, "%d", &number2);

   ASSERT (bson_iter_init_find (&iter, input, "i"));
   ASSERT (BSON_ITER_HOLDS_INT32 (&iter));
   number1 = bson_iter_int32 (&iter);

   ASSERT_CMPINT32 (number1, ==, number2);

   bson = BCON_NEW ("i", BCON_INT32 (number1));

   str1 = bson_as_json (bson, NULL);
   str2 = bson_as_json (json, NULL);
   ASSERT_CMPSTR (str1, str2);
   bson_free (str1);
   bson_free (str2);
   bson_free (bson);
}

void
test_bson_type_bool (const bson_t *input,
                     const char   *expected,
                     const bson_t *json,
                     bool          roundtrip_json)
{
   bool b;
   bson_t *bson;
   bson_iter_t iter;
   char *str1, *str2;

   BSON_ASSERT (input);
   BSON_ASSERT (expected);
   BSON_ASSERT (json);


   ASSERT (bson_iter_init_find (&iter, input, "b"));
   ASSERT (BSON_ITER_HOLDS_BOOL (&iter));
   b = bson_iter_bool (&iter);

   ASSERT_CMPSTR (expected, b ? "true" : "false");

   bson = BCON_NEW ("b", BCON_BOOL (b));

   str1 = bson_as_json (bson, NULL);
   str2 = bson_as_json (json, NULL);
   ASSERT_CMPSTR (str1, str2);
   bson_free (str1);
   bson_free (str2);
   bson_free (bson);
}

void
test_bson_type_decimal128 (const bson_t *input,
                           const char   *expected,
                           const bson_t *json,
                           bool          roundtrip_json)
{
   char bson_string[BSON_DECIMAL128_STRING];
   char json_string[BSON_DECIMAL128_STRING];
   bson_t *bson;
   bson_decimal128_t bson_decimal128;
   bson_decimal128_t json_decimal128;
   bson_iter_t iter;
   char *str1, *str2;

   BSON_ASSERT (input);
   BSON_ASSERT (expected);
   BSON_ASSERT (json);

   ASSERT (bson_iter_init_find (&iter, input, "d"));
   ASSERT (BSON_ITER_HOLDS_DECIMAL128 (&iter));
   ASSERT (bson_iter_decimal128 (&iter, &bson_decimal128));

   bson_decimal128_to_string (&bson_decimal128, bson_string);
   ASSERT_CMPSTR (bson_string, expected);

   bson = BCON_NEW ("d", BCON_DECIMAL128 (&bson_decimal128));

   ASSERT (bson_iter_init_find (&iter, input, "d"));
   ASSERT (BSON_ITER_HOLDS_DECIMAL128 (&iter));
   ASSERT (bson_iter_decimal128 (&iter, &json_decimal128));

   bson_decimal128_to_string (&json_decimal128, json_string);
   ASSERT_CMPSTR (json_string, expected);
   ASSERT_CMPSTR (json_string, bson_string);


   if (roundtrip_json) {
      str1 = bson_as_json (bson, NULL);
      str2 = bson_as_json (json, NULL);
      ASSERT_CMPSTR (str1, str2);
      bson_free (str1);
      bson_free (str2);
   }
   bson_free (bson);
}

static void
_test_bson_type_print_description (bson_iter_t *iter)
{
   if (bson_iter_find (iter, "description") && BSON_ITER_HOLDS_UTF8 (iter)) {
      if (test_suite_debug_output ()) {
         fprintf (stderr, "  - %s\n", bson_iter_utf8 (iter, NULL));
         fflush (stderr);
      }
   }
}

void
_test_bson_type_visit_corrupt (const bson_iter_t *iter,
                               void              *data)
{
   *((bool *) data) = true;
}

#define IS_NAN(dec) (dec).high == 0x7c00000000000000ull

static void
test_bson_type (bson_t *scenario, test_bson_type_valid_cb valid)
{
   bson_iter_t iter;
   bson_iter_t inner_iter;
   BSON_ASSERT (scenario);

   if (bson_iter_init_find (&iter, scenario, "valid")) {<commit_msg>unused test code<commit_after>/*
 * Copyright 2016 MongoDB, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


#include <bson.h>

#include "TestSuite.h"
#include "json-test.h"

#include <limits.h>

#ifdef _MSC_VER
#define PATH_MAX 1024
#define realpath(path, expanded) GetFullPathName (path, PATH_MAX, expanded, NULL)
#endif

typedef void (*test_bson_type_valid_cb)(const bson_t *input, const char *expected, const bson_t *json, bool roundtrip_json);

#ifdef _MSC_VER
# define SSCANF sscanf_s
#else
# define SSCANF sscanf
#endif

#ifndef JSON_DIR
# define JSON_DIR "tests/json"
#endif

void
test_bson_type_int32 (const bson_t *input,
                      const char   *expected,
                      const bson_t *json,
                      bool          roundtrip_json)
{
   int32_t number1, number2;
   bson_t *bson;
   bson_iter_t iter;
   char *str1, *str2;

   BSON_ASSERT (input);
   BSON_ASSERT (expected);
   BSON_ASSERT (json);


   SSCANF (expected, "%d", &number2);

   ASSERT (bson_iter_init_find (&iter, input, "i"));
   ASSERT (BSON_ITER_HOLDS_INT32 (&iter));
   number1 = bson_iter_int32 (&iter);

   ASSERT_CMPINT32 (number1, ==, number2);

   bson = BCON_NEW ("i", BCON_INT32 (number1));

   str1 = bson_as_json (bson, NULL);
   str2 = bson_as_json (json, NULL);
   ASSERT_CMPSTR (str1, str2);
   bson_free (str1);
   bson_free (str2);
   bson_free (bson);
}

void
test_bson_type_bool (const bson_t *input,
                     const char   *expected,
                     const bson_t *json,
                     bool          roundtrip_json)
{
   bool b;
   bson_t *bson;
   bson_iter_t iter;
   char *str1, *str2;

   BSON_ASSERT (input);
   BSON_ASSERT (expected);
   BSON_ASSERT (json);


   ASSERT (bson_iter_init_find (&iter, input, "b"));
   ASSERT (BSON_ITER_HOLDS_BOOL (&iter));
   b = bson_iter_bool (&iter);

   ASSERT_CMPSTR (expected, b ? "true" : "false");

   bson = BCON_NEW ("b", BCON_BOOL (b));

   str1 = bson_as_json (bson, NULL);
   str2 = bson_as_json (json, NULL);
   ASSERT_CMPSTR (str1, str2);
   bson_free (str1);
   bson_free (str2);
   bson_free (bson);
}

void
test_bson_type_decimal128 (const bson_t *input,
                           const char   *expected,
                           const bson_t *json,
                           bool          roundtrip_json)
{
   char bson_string[BSON_DECIMAL128_STRING];
   char json_string[BSON_DECIMAL128_STRING];
   bson_t *bson;
   bson_decimal128_t bson_decimal128;
   bson_decimal128_t json_decimal128;
   bson_iter_t iter;
   char *str1, *str2;

   BSON_ASSERT (input);
   BSON_ASSERT (expected);
   BSON_ASSERT (json);

   ASSERT (bson_iter_init_find (&iter, input, "d"));
   ASSERT (BSON_ITER_HOLDS_DECIMAL128 (&iter));
   ASSERT (bson_iter_decimal128 (&iter, &bson_decimal128));

   bson_decimal128_to_string (&bson_decimal128, bson_string);
   ASSERT_CMPSTR (bson_string, expected);

   bson = BCON_NEW ("d", BCON_DECIMAL128 (&bson_decimal128));

   ASSERT (bson_iter_init_find (&iter, input, "d"));
   ASSERT (BSON_ITER_HOLDS_DECIMAL128 (&iter));
   ASSERT (bson_iter_decimal128 (&iter, &json_decimal128));

   bson_decimal128_to_string (&json_decimal128, json_string);
   ASSERT_CMPSTR (json_string, expected);
   ASSERT_CMPSTR (json_string, bson_string);


   if (roundtrip_json) {
      str1 = bson_as_json (bson, NULL);
      str2 = bson_as_json (json, NULL);
      ASSERT_CMPSTR (str1, str2);
      bson_free (str1);
      bson_free (str2);
   }
   bson_free (bson);
}

static void
_test_bson_type_print_description (bson_iter_t *iter)
{
   if (bson_iter_find (iter, "description") && BSON_ITER_HOLDS_UTF8 (iter)) {
      if (test_suite_debug_output ()) {
         fprintf (stderr, "  - %s\n", bson_iter_utf8 (iter, NULL));
         fflush (stderr);
      }
   }
}

#define IS_NAN(dec) (dec).high == 0x7c00000000000000ull

static void
test_bson_type (bson_t *scenario, test_bson_type_valid_cb valid)
{
   bson_iter_t iter;
   bson_iter_t inner_iter;
   BSON_ASSERT (scenario);

   if (bson_iter_init_find (&iter, scenario, "valid")) {
{'fraction_non_alphanumeric': 0.10830363891588381, 'fraction_numerical': 0.028074721952272973, 'mean_word_length': 3.3019043195541107, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6673097', 'n_tokens_mistral': 3589, 'n_tokens_neox': 3382, 'n_words': 830}