<commit_before>/*ckwg +5
 * Copyright 2011-2012 by Kitware, Inc. All Rights Reserved. Please refer to
 * KITWARE_LICENSE.TXT for licensing information, or contact General Counsel,
 * Kitware, Inc., 28 Corporate Drive, Clifton Park, NY 12065.
 */

#include <test_common.h>

#include <vistk/pipeline/config.h>

#include <exception>
#include <iostream>
#include <string>

#include <cstdlib>

static void run_test(std::string const& test_name);

int
main(int argc, char* argv[])
{
  if (argc != 2)
  {
    TEST_ERROR("Expected one argument");

    return EXIT_FAILURE;
  }

  std::string const test_name = argv[1];

  try
  {
    run_test(test_name);
  }
  catch (std::exception& e)
  {
    TEST_ERROR("Unexpected exception: " << e.what());

    return EXIT_FAILURE;
  }

  return EXIT_SUCCESS;
}

static void test_has_value();
static void test_get_value();
static void test_get_value_no_exist();
static void test_get_value_type_mismatch();
static void test_bool_conversion();
static void test_unset_value();
static void test_available_values();
static void test_read_only();
static void test_read_only_unset();
static void test_subblock();
static void test_subblock_view();
static void test_merge_config();

void
run_test(std::string const& test_name)
{
  if (test_name == "has_value")
  {
    test_has_value();
  }
  else if (test_name == "get_value")
  {
    test_get_value();
  }
  else if (test_name == "get_value_no_exist")
  {
    test_get_value_no_exist();
  }
  else if (test_name == "get_value_type_mismatch")
  {
    test_get_value_type_mismatch();
  }
  else if (test_name == "bool_conversion")
  {
    test_bool_conversion();
  }
  else if (test_name == "unset_value")
  {
    test_unset_value();
  }
  else if (test_name == "available_values")
  {
    test_available_values();
  }
  else if (test_name == "read_only")
  {
    test_read_only();
  }
  else if (test_name == "read_only_unset")
  {
    test_read_only_unset();
  }
  else if (test_name == "subblock")
  {
    test_subblock();
  }
  else if (test_name == "subblock_view")
  {
    test_subblock_view();
  }
  else if (test_name == "merge_config")
  {
    test_merge_config();
  }
  else
  {
    TEST_ERROR("Unknown test: " << test_name);
  }
}

void
test_has_value()
{
  vistk::config_t config = vistk::config::empty_config();

  vistk::config::key_t const keya = vistk::config::key_t("keya");
  vistk::config::key_t const keyb = vistk::config::key_t("keyb");

  vistk::config::value_t const valuea = vistk::config::value_t("value_a");

  config->set_value(keya, valuea);

  if (!config->has_value(keya))
  {
    TEST_ERROR("Block does not have value which was set");
  }

  if (config->has_value(keyb))
  {
    TEST_ERROR("Block has value which was not set");
  }
}

void
test_get_value()
{
  vistk::config_t config = vistk::config::empty_config();

  vistk::config::key_t const keya = vistk::config::key_t("keya");

  vistk::config::value_t const valuea = vistk::config::value_t("value_a");

  config->set_value(keya, valuea);

  vistk::config::value_t const get_valuea = config->get_value<vistk::config::value_t>(keya);

  if (valuea != get_valuea)
  {
    TEST_ERROR("Did not retrieve value that was set");
  }
}

void
test_get_value_no_exist()
{
  vistk::config_t config = vistk::config::empty_config();

  vistk::config::key_t const keya = vistk::config::key_t("keya");
  vistk::config::key_t const keyb = vistk::config::key_t("keyb");

  vistk::config::value_t const valueb = vistk::config::value_t("value_b");

  EXPECT_EXCEPTION(vistk::no_such_configuration_value_exception,
                   config->get_value<vistk::config::value_t>(keya),
                   "retrieving an unset value");

  vistk::config::value_t const get_valueb = config->get_value<vistk::config::value_t>(keyb, valueb);

  if (valueb != get_valueb)
  {
    TEST_ERROR("Did not retrieve default when requesting unset value");
  }
}

void
test_get_value_type_mismatch()
{
  vistk::config_t config = vistk::config::empty_config();

  vistk::config::key_t const keya = vistk::config::key_t("keya");

  vistk::config::value_t const valuea = vistk::config::value_t("value_a");
  int const valueb = 100;

  config->set_value(keya, valuea);

  EXPECT_EXCEPTION(vistk::bad_configuration_cast_exception,
                   config->get_value<int>(keya),
                   "doing an invalid cast");

  int const get_valueb = config->get_value<int>(keya, valueb);

  if (valueb != get_valueb)
  {
    TEST_ERROR("Did not retrieve default when requesting a bad cast");
  }
}

void
test_bool_conversion()
{
  vistk::config_t config = vistk::config::empty_config();

  vistk::config::key_t const key = vistk::config::key_t("key");

  vistk::config::value_t const lit_true = vistk::config::value_t("true");
  vistk::config::value_t const lit_false = vistk::config::value_t("false");
  vistk::config::value_t const lit_1 = vistk::config::value_t("1");
  vistk::config::value_t const lit_0 = vistk::config::value_t("0");

  bool val;

  config->set_value(key, lit_true);
  val = config->get_value<bool>(key);

  if (!val)
  {
    TEST_ERROR("The value \'true\' did not get converted to true when read as a boolean");
  }

  config->set_value(key, lit_false);
  val = config->get_value<bool>(key);

  if (val)
  {
    TEST_ERROR("The value \'false\' did not get converted to false when read as a boolean");
  }

  config->set_value(key, lit_1);
  val = config->get_value<bool>(key);

  if (!val)
  {
    TEST_ERROR("The value \'1\' did not get converted to true when read as a boolean");
  }

  config->set_value(key, lit_0);
  val = config->get_value<bool>(key);

  if (val)
  {
    TEST_ERROR("The value \'0\' did not get converted to true when read as a boolean");<commit_msg>Add tests for True and False bool conversions<commit_after>/*ckwg +5
 * Copyright 2011-2012 by Kitware, Inc. All Rights Reserved. Please refer to
 * KITWARE_LICENSE.TXT for licensing information, or contact General Counsel,
 * Kitware, Inc., 28 Corporate Drive, Clifton Park, NY 12065.
 */

#include <test_common.h>

#include <vistk/pipeline/config.h>

#include <exception>
#include <iostream>
#include <string>

#include <cstdlib>

static void run_test(std::string const& test_name);

int
main(int argc, char* argv[])
{
  if (argc != 2)
  {
    TEST_ERROR("Expected one argument");

    return EXIT_FAILURE;
  }

  std::string const test_name = argv[1];

  try
  {
    run_test(test_name);
  }
  catch (std::exception& e)
  {
    TEST_ERROR("Unexpected exception: " << e.what());

    return EXIT_FAILURE;
  }

  return EXIT_SUCCESS;
}

static void test_has_value();
static void test_get_value();
static void test_get_value_no_exist();
static void test_get_value_type_mismatch();
static void test_bool_conversion();
static void test_unset_value();
static void test_available_values();
static void test_read_only();
static void test_read_only_unset();
static void test_subblock();
static void test_subblock_view();
static void test_merge_config();

void
run_test(std::string const& test_name)
{
  if (test_name == "has_value")
  {
    test_has_value();
  }
  else if (test_name == "get_value")
  {
    test_get_value();
  }
  else if (test_name == "get_value_no_exist")
  {
    test_get_value_no_exist();
  }
  else if (test_name == "get_value_type_mismatch")
  {
    test_get_value_type_mismatch();
  }
  else if (test_name == "bool_conversion")
  {
    test_bool_conversion();
  }
  else if (test_name == "unset_value")
  {
    test_unset_value();
  }
  else if (test_name == "available_values")
  {
    test_available_values();
  }
  else if (test_name == "read_only")
  {
    test_read_only();
  }
  else if (test_name == "read_only_unset")
  {
    test_read_only_unset();
  }
  else if (test_name == "subblock")
  {
    test_subblock();
  }
  else if (test_name == "subblock_view")
  {
    test_subblock_view();
  }
  else if (test_name == "merge_config")
  {
    test_merge_config();
  }
  else
  {
    TEST_ERROR("Unknown test: " << test_name);
  }
}

void
test_has_value()
{
  vistk::config_t config = vistk::config::empty_config();

  vistk::config::key_t const keya = vistk::config::key_t("keya");
  vistk::config::key_t const keyb = vistk::config::key_t("keyb");

  vistk::config::value_t const valuea = vistk::config::value_t("value_a");

  config->set_value(keya, valuea);

  if (!config->has_value(keya))
  {
    TEST_ERROR("Block does not have value which was set");
  }

  if (config->has_value(keyb))
  {
    TEST_ERROR("Block has value which was not set");
  }
}

void
test_get_value()
{
  vistk::config_t config = vistk::config::empty_config();

  vistk::config::key_t const keya = vistk::config::key_t("keya");

  vistk::config::value_t const valuea = vistk::config::value_t("value_a");

  config->set_value(keya, valuea);

  vistk::config::value_t const get_valuea = config->get_value<vistk::config::value_t>(keya);

  if (valuea != get_valuea)
  {
    TEST_ERROR("Did not retrieve value that was set");
  }
}

void
test_get_value_no_exist()
{
  vistk::config_t config = vistk::config::empty_config();

  vistk::config::key_t const keya = vistk::config::key_t("keya");
  vistk::config::key_t const keyb = vistk::config::key_t("keyb");

  vistk::config::value_t const valueb = vistk::config::value_t("value_b");

  EXPECT_EXCEPTION(vistk::no_such_configuration_value_exception,
                   config->get_value<vistk::config::value_t>(keya),
                   "retrieving an unset value");

  vistk::config::value_t const get_valueb = config->get_value<vistk::config::value_t>(keyb, valueb);

  if (valueb != get_valueb)
  {
    TEST_ERROR("Did not retrieve default when requesting unset value");
  }
}

void
test_get_value_type_mismatch()
{
  vistk::config_t config = vistk::config::empty_config();

  vistk::config::key_t const keya = vistk::config::key_t("keya");

  vistk::config::value_t const valuea = vistk::config::value_t("value_a");
  int const valueb = 100;

  config->set_value(keya, valuea);

  EXPECT_EXCEPTION(vistk::bad_configuration_cast_exception,
                   config->get_value<int>(keya),
                   "doing an invalid cast");

  int const get_valueb = config->get_value<int>(keya, valueb);

  if (valueb != get_valueb)
  {
    TEST_ERROR("Did not retrieve default when requesting a bad cast");
  }
}

void
test_bool_conversion()
{
  vistk::config_t config = vistk::config::empty_config();

  vistk::config::key_t const key = vistk::config::key_t("key");

  vistk::config::value_t const lit_true = vistk::config::value_t("true");
  vistk::config::value_t const lit_false = vistk::config::value_t("false");
  vistk::config::value_t const lit_True = vistk::config::value_t("True");
  vistk::config::value_t const lit_False = vistk::config::value_t("False");
  vistk::config::value_t const lit_1 = vistk::config::value_t("1");
  vistk::config::value_t const lit_0 = vistk::config::value_t("0");

  bool val;

  config->set_value(key, lit_true);
  val = config->get_value<bool>(key);

  if (!val)
  {
    TEST_ERROR("The value \'true\' did not get converted to true when read as a boolean");
  }

  config->set_value(key, lit_false);
  val = config->get_value<bool>(key);

  if (val)
  {
    TEST_ERROR("The value \'false\' did not get converted to false when read as a boolean");
  }

  config->set_value(key, lit_True);
  val = config->get_value<bool>(key);

  if (!val)
  {
    TEST_ERROR("The value \'True\' did not get converted to true when read as a boolean");
  }

  config->set_value(key, lit_False);
  val = config->get_value<bool>(key);

  if (val)
  {
    TEST_ERROR("The value \'False\' did not get converted to false when read as a boolean");
  }

  config->set_value(key, lit_1);
  val = config->get_value<bool>(key);

  if (!val)
  {
    TEST_ERROR("The value \'1\' did not get converted to true when read as a boolean");
  }

  config->set_value(key, lit_0);
  val = config->get_value<bool>(key);

  if (val)
  {
    TEST_ERROR("The value \'0\' did not get converted to true when read as a boolean");
{'fraction_non_alphanumeric': 0.14594459279038718, 'fraction_numerical': 0.0048397863818424565, 'mean_word_length': 4.413279132791328, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4423888', 'n_tokens_mistral': 4489, 'n_tokens_neox': 4287, 'n_words': 922}