<commit_before>/*ckwg +5
 * Copyright 2011 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 <vistk/pipeline/modules.h>
#include <vistk/pipeline/process.h>
#include <vistk/pipeline/process_exception.h>
#include <vistk/pipeline/process_registry.h>
#include <vistk/pipeline/process_registry_exception.h>
#include <vistk/pipeline/types.h>

#include <boost/foreach.hpp>

#include <exception>
#include <iostream>

static void test_process(vistk::process_registry::type_t const& type);

int
main()
{
  try
  {
    vistk::load_known_modules();
  }
  catch (vistk::process_type_already_exists_exception& e)
  {
    std::cerr << "Error: Duplicate process names: " << e.what() << std::endl;
  }
  catch (vistk::pipeline_exception& e)
  {
    std::cerr << "Error: Failed to load modules: " << e.what() << std::endl;
  }
  catch (std::exception& e)
  {
    std::cerr << "Error: Unexpected exception when loading modules: " << e.what() << std::endl;

    return 1;
  }

  vistk::process_registry_t const reg = vistk::process_registry::self();

  vistk::process_registry::types_t const types = reg->types();

  BOOST_FOREACH (vistk::process_registry::type_t const& type, types)
  {
    try
    {
      test_process(type);
    }
    catch (std::exception& e)
    {
      std::cerr << "Error: Unexpected exception when testing "
                << "type \'" << type << "\': " << e.what() << std::endl;
    }
  }

  return 0;
}

static void test_process_configuration(vistk::process_t const process);
static void test_process_input_ports(vistk::process_t const process);
static void test_process_output_ports(vistk::process_t const process);

static void test_process_invalid_configuration(vistk::process_t const process);
static void test_process_invalid_input_port(vistk::process_t const process);
static void test_process_invalid_output_port(vistk::process_t const process);

void
test_process(vistk::process_registry::type_t const& type)
{
  static vistk::process::name_t const expected_name = vistk::process::name_t("expected_name");

  vistk::config_t config = vistk::config::empty_config();
  config->set_value(vistk::process::config_name, expected_name);

  vistk::process_registry_t const reg = vistk::process_registry::self();

  if (reg->description(type).empty())
  {
    std::cerr << "Error: The description is empty" << std::endl;
  }

  vistk::process_t const process = reg->create_process(type, config);

  if (!process)
  {
    std::cerr << "Error: Received NULL process (" << type << ")" << std::endl;

    return;
  }

  if (process->name() != expected_name)
  {
    std::cerr << "Error: Name (" << process->name() << ") "
              << "does not match expected name: " << expected_name << std::endl;
  }

  if (process->type() != type)
  {
    std::cerr << "Error: The type does not match the registry type" << std::endl;
  }

  test_process_configuration(process);
  test_process_input_ports(process);
  test_process_output_ports(process);

  test_process_invalid_configuration(process);
  test_process_invalid_input_port(process);
  test_process_invalid_output_port(process);
}

void
test_process_configuration(vistk::process_t const process)
{
  vistk::config::keys_t const keys = process->available_config();

  BOOST_FOREACH (vistk::config::key_t const& key, keys)
  {
    try
    {
      process->config_info(key);
    }
    catch (vistk::unknown_configuration_value_exception& e)
    {
      std::cerr << "Error: Failed to get a default for "
                << process->type() << vistk::config::block_sep << key
                << ": " << e.what() << std::endl;
    }
    catch (std::exception& e)
    {
      std::cerr << "Error: Unexpected exception when querying for default "
                << "(" << process->type() << vistk::config::block_sep
                << key << "): " << e.what() << std::endl;
    }
  }
}

void
test_process_input_ports(vistk::process_t const process)
{
  vistk::process::ports_t const ports = process->input_ports();

  BOOST_FOREACH (vistk::process::port_t const& port, ports)
  {
    vistk::process::port_info_t info;

    try
    {
      info = process->input_port_info(port);
    }
    catch (vistk::no_such_port_exception& e)
    {
      std::cerr << "Error: Failed to get a info for input port "
                << process->type() << "." << port << ": " << e.what() << std::endl;
    }
    catch (std::exception& e)
    {
      std::cerr << "Error: Unexpected exception when querying for input port info "
                << "(" << process->type() << "." << port << "): " << e.what() << std::endl;
    }

    vistk::process::port_flags_t const& flags = info->flags;

    bool const is_const = (flags.find(vistk::process::flag_output_const) != flags.end());

    if (is_const)
    {
      std::cerr << "Error: Const flag on input port "
                << "(" << process->type() << "." << port << ")" << std::endl;
    }

    vistk::process::port_description_t const& description = info->description;

    if (description.empty())
    {
      std::cerr << "Error: Description empty on input port "
                << "(" << process->type() << "." << port << ")" << std::endl;
    }
  }
}

void
test_process_output_ports(vistk::process_t const process)
{
  vistk::process::ports_t const ports = process->output_ports();

  BOOST_FOREACH (vistk::process::port_t const& port, ports)
  {
    vistk::process::port_info_t info;

    try
    {
      info = process->output_port_info(port);
    }
    catch (vistk::no_such_port_exception& e)<commit_msg>Test for reconnect exceptions on input ports<commit_after>/*ckwg +5
 * Copyright 2011 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 <vistk/pipeline/modules.h>
#include <vistk/pipeline/process.h>
#include <vistk/pipeline/process_exception.h>
#include <vistk/pipeline/process_registry.h>
#include <vistk/pipeline/process_registry_exception.h>
#include <vistk/pipeline/types.h>

#include <boost/foreach.hpp>

#include <exception>
#include <iostream>

static void test_process(vistk::process_registry::type_t const& type);

int
main()
{
  try
  {
    vistk::load_known_modules();
  }
  catch (vistk::process_type_already_exists_exception& e)
  {
    std::cerr << "Error: Duplicate process names: " << e.what() << std::endl;
  }
  catch (vistk::pipeline_exception& e)
  {
    std::cerr << "Error: Failed to load modules: " << e.what() << std::endl;
  }
  catch (std::exception& e)
  {
    std::cerr << "Error: Unexpected exception when loading modules: " << e.what() << std::endl;

    return 1;
  }

  vistk::process_registry_t const reg = vistk::process_registry::self();

  vistk::process_registry::types_t const types = reg->types();

  BOOST_FOREACH (vistk::process_registry::type_t const& type, types)
  {
    try
    {
      test_process(type);
    }
    catch (std::exception& e)
    {
      std::cerr << "Error: Unexpected exception when testing "
                << "type \'" << type << "\': " << e.what() << std::endl;
    }
  }

  return 0;
}

static void test_process_configuration(vistk::process_t const process);
static void test_process_input_ports(vistk::process_t const process);
static void test_process_output_ports(vistk::process_t const process);

static void test_process_invalid_configuration(vistk::process_t const process);
static void test_process_invalid_input_port(vistk::process_t const process);
static void test_process_invalid_output_port(vistk::process_t const process);

void
test_process(vistk::process_registry::type_t const& type)
{
  static vistk::process::name_t const expected_name = vistk::process::name_t("expected_name");

  vistk::config_t config = vistk::config::empty_config();
  config->set_value(vistk::process::config_name, expected_name);

  vistk::process_registry_t const reg = vistk::process_registry::self();

  if (reg->description(type).empty())
  {
    std::cerr << "Error: The description is empty" << std::endl;
  }

  vistk::process_t const process = reg->create_process(type, config);

  if (!process)
  {
    std::cerr << "Error: Received NULL process (" << type << ")" << std::endl;

    return;
  }

  if (process->name() != expected_name)
  {
    std::cerr << "Error: Name (" << process->name() << ") "
              << "does not match expected name: " << expected_name << std::endl;
  }

  if (process->type() != type)
  {
    std::cerr << "Error: The type does not match the registry type" << std::endl;
  }

  test_process_configuration(process);
  test_process_input_ports(process);
  test_process_output_ports(process);

  test_process_invalid_configuration(process);
  test_process_invalid_input_port(process);
  test_process_invalid_output_port(process);
}

void
test_process_configuration(vistk::process_t const process)
{
  vistk::config::keys_t const keys = process->available_config();

  BOOST_FOREACH (vistk::config::key_t const& key, keys)
  {
    try
    {
      process->config_info(key);
    }
    catch (vistk::unknown_configuration_value_exception& e)
    {
      std::cerr << "Error: Failed to get a default for "
                << process->type() << vistk::config::block_sep << key
                << ": " << e.what() << std::endl;
    }
    catch (std::exception& e)
    {
      std::cerr << "Error: Unexpected exception when querying for default "
                << "(" << process->type() << vistk::config::block_sep
                << key << "): " << e.what() << std::endl;
    }
  }
}

void
test_process_input_ports(vistk::process_t const process)
{
  static vistk::config_t const config = vistk::config::empty_config();

  vistk::process::ports_t const ports = process->input_ports();

  BOOST_FOREACH (vistk::process::port_t const& port, ports)
  {
    vistk::process::port_info_t info;

    try
    {
      info = process->input_port_info(port);
    }
    catch (vistk::no_such_port_exception& e)
    {
      std::cerr << "Error: Failed to get a info for input port "
                << process->type() << "." << port << ": " << e.what() << std::endl;
    }
    catch (std::exception& e)
    {
      std::cerr << "Error: Unexpected exception when querying for input port info "
                << "(" << process->type() << "." << port << "): " << e.what() << std::endl;
    }

    vistk::process::port_flags_t const& flags = info->flags;

    bool const is_const = (flags.find(vistk::process::flag_output_const) != flags.end());

    if (is_const)
    {
      std::cerr << "Error: Const flag on input port "
                << "(" << process->type() << "." << port << ")" << std::endl;
    }

    vistk::process::port_description_t const& description = info->description;

    if (description.empty())
    {
      std::cerr << "Error: Description empty on input port "
                << "(" << process->type() << "." << port << ")" << std::endl;
    }

    vistk::edge_t edge = vistk::edge_t(new vistk::edge(config));

    process->connect_input_port(port, edge);

    EXPECT_EXCEPTION(vistk::port_reconnect_exception,
                     process->connect_input_port(port, edge),
                     "connecting to an input port a second time");
  }
}

void
test_process_output_ports(vistk::process_t const process)
{
  vistk::process::ports_t const ports = process->output_ports();

  BOOST_FOREACH (vistk::process::port_t const& port, ports)
  {
    vistk::process::port_info_t info;

    try
    {
      info = process->output_port_info(port);
    }
    catch (vistk::no_such_port_exception& e)
{'fraction_non_alphanumeric': 0.15284470369431058, 'fraction_numerical': 0.002367063995265872, 'mean_word_length': 3.715025906735751, 'pattern_counts': {'":': 4, '<': 317, '<?xml version=': 0, '>': 69, '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': '4435801', 'n_tokens_mistral': 4009, 'n_tokens_neox': 3809, 'n_words': 805}