<commit_before>$LOAD_PATH.unshift File.expand_path('../lib', File.dirname(__FILE__))
require 'orogen/test'

class TC_GenerationTypekit < Test::Unit::TestCase
    include Orocos::Generation::Test

    def test_orocos_type_equivalence
	registry = Typelib::Registry.new

	assert_equal(registry.get('int'), registry.base_rtt_type_for(registry.get('int32_t')))
	assert_equal(registry.get('unsigned int'), registry.base_rtt_type_for(registry.get('uint32_t')))
	assert_equal(registry.get('int'), registry.base_rtt_type_for(registry.get('int16_t')))
	assert_equal(registry.get('unsigned int'), registry.base_rtt_type_for(registry.get('uint16_t')))
	assert_equal(registry.get('int'), registry.base_rtt_type_for(registry.get('short')))
	assert_equal(registry.get('unsigned int'), registry.base_rtt_type_for(registry.get('unsigned short')))
	assert_equal(registry.get('double'), registry.base_rtt_type_for(registry.get('float')))
    end

    def test_typekit_load
	component = Component.new
        component.name 'test_typekit_load'

        # Load a file that does not exist
        assert_raises(ArgumentError) do
            component.typekit do
                load 'does_not_exist.h'
            end
        end

        # Load a file with errors
        assert_raises(ArgumentError) do
            typekit = component.typekit(true)
            typekit.load File.join(TEST_DATA_DIR, 'exists')
            typekit.perform_pending_loads
        end
    end

    def check_output_file(basedir, name)
        output   = File.read(File.join(prefix_directory, name))
        expected = File.read(File.join(TEST_DATA_DIR, basedir, name))
        assert_equal(expected, output)
    end

    def test_opaque(*transports)
        build_test_component('modules/typekit_opaque', transports, "bin/test") do |cmake|
            transports.each do |transport_name|
                cmake << "ADD_DEFINITIONS(-DWITH_#{transport_name.upcase})\n"
            end

            cmake << <<-EOF
link_directories(${CMAKE_INSTALL_PREFIX}/lib/orocos/plugins ${CMAKE_INSTALL_PREFIX}/lib/orocos/types)

ADD_EXECUTABLE(test test.cpp)
list(APPEND CMAKE_PREFIX_PATH ${OrocosRTT_PREFIX})
target_link_libraries(test opaque-typekit-${OROCOS_TARGET})
target_link_libraries(test ${OROCOS_COMPONENT_LIBRARIES})
include_directories(${CMAKE_SOURCE_DIR}/.orogen/typekit)
include_directories(${CMAKE_BINARY_DIR}/.orogen/typekit)
find_package( RTTPlugin COMPONENTS rtt-typekit rtt-marshalling)
target_link_libraries(test ${RTT_PLUGIN_rtt-marshalling_LIBRARY})
target_link_libraries(test ${RTT_PLUGIN_rtt-typekit_LIBRARY})
INSTALL(TARGETS test RUNTIME DESTINATION bin)
            EOF

            transports.each do |transport_name|
                cmake << "\ntarget_link_libraries(test opaque-transport-#{transport_name}-${OROCOS_TARGET})"
            end
	end

        #check_output_file('modules/typekit_opaque', 'opaque.xml')
        #check_output_file('modules/typekit_opaque', 'opaque.cpf')
        #check_output_file('modules/typekit_opaque', 'composed_opaque.xml')
        #check_output_file('modules/typekit_opaque', 'composed_opaque.cpf')
        #check_output_file('modules/typekit_opaque', 'shared_ptr__opaque_type.xml')
        #check_output_file('modules/typekit_opaque', 'shared_ptr__opaque_type.cpf')
        #check_output_file('modules/typekit_opaque', 'shared_ptr__shared_ptr.xml')
        #check_output_file('modules/typekit_opaque', 'shared_ptr__shared_ptr.cpf')
        #check_output_file('modules/typekit_opaque', 'readonlypointer.xml')
        #check_output_file('modules/typekit_opaque', 'readonlypointer.cpf')
    end
    def test_opaque_corba; test_opaque('corba') end
    def test_opaque_typelib; test_opaque('typelib') end

    def test_opaque_autodef
        build_test_component('modules/typekit_autodef', ['corba'])
    end

    def test_opaque_validation
        # First, check that the actual opaque module generates properly
        create_wc("modules/typekit_opaque_validation_ok")
        FileUtils.rm_rf working_directory
        FileUtils.cp_r File.join(TEST_DATA_DIR, "modules/typekit_opaque"), working_directory

        component = Component.new
        in_wc do
            component.load 'opaque.orogen'
            assert_nothing_raised { component.generate }
        end

        # Second, check that it fails if an invalid file is loaded
        create_wc("modules/typekit_opaque_validation_fail")
        FileUtils.rm_rf working_directory
        FileUtils.cp_r File.join(TEST_DATA_DIR, "modules/typekit_opaque"), working_directory

        component = Component.new
        in_wc do
            component.load 'opaque.orogen'
            component.typekit(true).load File.join(TEST_DATA_DIR, 'opaque_invalid.h')
            assert_raises(NotImplementedError) { component.generate }
        end
    end

    def test_simple(*transports)
        build_test_component('modules/typekit_simple', transports, "bin/test") do |cmake|
            ENV['CMAKE_LIBRARY_PATH'] = "#{ENV['CMAKE_LIBRARY_PATH']}:#{prefix_directory}"

            transports.each do |transport_name|
                cmake << "ADD_DEFINITIONS(-DWITH_#{transport_name.upcase})\n"
            end
            cmake << <<-EOT
include_directories(${OrocosRTT_INCLUDE_DIRS} ${OrocosCORBA_INCLUDE_DIRS})
include_directories(${CMAKE_SOURCE_DIR}/.orogen/typekit)
include_directories(${CMAKE_BINARY_DIR}/.orogen/typekit)
link_directories(${OrocosCORBA_LIBRARY_DIRS} ${OrocosRTT_LIBRARY_DIRS})
link_directories(${CMAKE_INSTALL_PREFIX}/lib/orocos/plugins ${CMAKE_INSTALL_PREFIX}/lib/orocos/types)
list(APPEND CMAKE_PREFIX_PATH ${OrocosRTT_PREFIX})

add_executable(test test.cpp)
target_link_libraries(test simple-typekit-${OROCOS_TARGET})
target_link_libraries(test ${OrocosRTT_LIBRARIES})
find_package( RTTPlugin COMPONENTS rtt-typekit rtt-marshalling)
target_link_libraries(test ${RTT_PLUGIN_rtt-marshalling_LIBRARY})
target_link_libraries(test ${RTT_PLUGIN_rtt-typekit_LIBRARY})
install(TARGETS test RUNTIME DESTINATION bin)
            EOT

            transports.each do |transport_name|
                cmake << "target_link_libraries(test simple-transport-#{transport_name}-${OROCOS_TARGET})\n"<commit_msg>test: need to add include and link options to the test programs as well in case the typelib transport is enabled<commit_after>$LOAD_PATH.unshift File.expand_path('../lib', File.dirname(__FILE__))
require 'orogen/test'

class TC_GenerationTypekit < Test::Unit::TestCase
    include Orocos::Generation::Test

    def test_orocos_type_equivalence
	registry = Typelib::Registry.new

	assert_equal(registry.get('int'), registry.base_rtt_type_for(registry.get('int32_t')))
	assert_equal(registry.get('unsigned int'), registry.base_rtt_type_for(registry.get('uint32_t')))
	assert_equal(registry.get('int'), registry.base_rtt_type_for(registry.get('int16_t')))
	assert_equal(registry.get('unsigned int'), registry.base_rtt_type_for(registry.get('uint16_t')))
	assert_equal(registry.get('int'), registry.base_rtt_type_for(registry.get('short')))
	assert_equal(registry.get('unsigned int'), registry.base_rtt_type_for(registry.get('unsigned short')))
	assert_equal(registry.get('double'), registry.base_rtt_type_for(registry.get('float')))
    end

    def test_typekit_load
	component = Component.new
        component.name 'test_typekit_load'

        # Load a file that does not exist
        assert_raises(ArgumentError) do
            component.typekit do
                load 'does_not_exist.h'
            end
        end

        # Load a file with errors
        assert_raises(ArgumentError) do
            typekit = component.typekit(true)
            typekit.load File.join(TEST_DATA_DIR, 'exists')
            typekit.perform_pending_loads
        end
    end

    def check_output_file(basedir, name)
        output   = File.read(File.join(prefix_directory, name))
        expected = File.read(File.join(TEST_DATA_DIR, basedir, name))
        assert_equal(expected, output)
    end

    def test_opaque(*transports)
        build_test_component('modules/typekit_opaque', transports, "bin/test") do |cmake|
            transports.each do |transport_name|
                cmake << "ADD_DEFINITIONS(-DWITH_#{transport_name.upcase})\n"
            end

            if transports.include?('typelib')
                cmake << <<-EOT
pkg_check_modules(TYPELIB REQUIRED typelib)
include_directories(${TYPELIB_INCLUDE_DIRS})
link_directories(${TYPELIB_LIBRARY_DIRS})
                EOT
            end

            cmake << <<-EOF
link_directories(${CMAKE_INSTALL_PREFIX}/lib/orocos/plugins ${CMAKE_INSTALL_PREFIX}/lib/orocos/types)

ADD_EXECUTABLE(test test.cpp)
list(APPEND CMAKE_PREFIX_PATH ${OrocosRTT_PREFIX})
target_link_libraries(test opaque-typekit-${OROCOS_TARGET})
target_link_libraries(test ${OROCOS_COMPONENT_LIBRARIES})
include_directories(${CMAKE_SOURCE_DIR}/.orogen/typekit)
include_directories(${CMAKE_BINARY_DIR}/.orogen/typekit)
find_package( RTTPlugin COMPONENTS rtt-typekit rtt-marshalling)
target_link_libraries(test ${RTT_PLUGIN_rtt-marshalling_LIBRARY})
target_link_libraries(test ${RTT_PLUGIN_rtt-typekit_LIBRARY})
INSTALL(TARGETS test RUNTIME DESTINATION bin)
            EOF

            transports.each do |transport_name|
                cmake << "\ntarget_link_libraries(test opaque-transport-#{transport_name}-${OROCOS_TARGET})"
            end
	end

        #check_output_file('modules/typekit_opaque', 'opaque.xml')
        #check_output_file('modules/typekit_opaque', 'opaque.cpf')
        #check_output_file('modules/typekit_opaque', 'composed_opaque.xml')
        #check_output_file('modules/typekit_opaque', 'composed_opaque.cpf')
        #check_output_file('modules/typekit_opaque', 'shared_ptr__opaque_type.xml')
        #check_output_file('modules/typekit_opaque', 'shared_ptr__opaque_type.cpf')
        #check_output_file('modules/typekit_opaque', 'shared_ptr__shared_ptr.xml')
        #check_output_file('modules/typekit_opaque', 'shared_ptr__shared_ptr.cpf')
        #check_output_file('modules/typekit_opaque', 'readonlypointer.xml')
        #check_output_file('modules/typekit_opaque', 'readonlypointer.cpf')
    end
    def test_opaque_corba; test_opaque('corba') end
    def test_opaque_typelib; test_opaque('typelib') end

    def test_opaque_autodef
        build_test_component('modules/typekit_autodef', ['corba'])
    end

    def test_opaque_validation
        # First, check that the actual opaque module generates properly
        create_wc("modules/typekit_opaque_validation_ok")
        FileUtils.rm_rf working_directory
        FileUtils.cp_r File.join(TEST_DATA_DIR, "modules/typekit_opaque"), working_directory

        component = Component.new
        in_wc do
            component.load 'opaque.orogen'
            assert_nothing_raised { component.generate }
        end

        # Second, check that it fails if an invalid file is loaded
        create_wc("modules/typekit_opaque_validation_fail")
        FileUtils.rm_rf working_directory
        FileUtils.cp_r File.join(TEST_DATA_DIR, "modules/typekit_opaque"), working_directory

        component = Component.new
        in_wc do
            component.load 'opaque.orogen'
            component.typekit(true).load File.join(TEST_DATA_DIR, 'opaque_invalid.h')
            assert_raises(NotImplementedError) { component.generate }
        end
    end

    def test_simple(*transports)
        build_test_component('modules/typekit_simple', transports, "bin/test") do |cmake|
            ENV['CMAKE_LIBRARY_PATH'] = "#{ENV['CMAKE_LIBRARY_PATH']}:#{prefix_directory}"

            transports.each do |transport_name|
                cmake << "ADD_DEFINITIONS(-DWITH_#{transport_name.upcase})\n"
            end
            if transports.include?('typelib')
                cmake << <<-EOT
pkg_check_modules(TYPELIB REQUIRED typelib)
include_directories(${TYPELIB_INCLUDE_DIRS})
link_directories(${TYPELIB_LIBRARY_DIRS})
                EOT
            end
            cmake << <<-EOT
include_directories(${OrocosRTT_INCLUDE_DIRS} ${OrocosCORBA_INCLUDE_DIRS})
include_directories(${CMAKE_SOURCE_DIR}/.orogen/typekit)
include_directories(${CMAKE_BINARY_DIR}/.orogen/typekit)
link_directories(${OrocosCORBA_LIBRARY_DIRS} ${OrocosRTT_LIBRARY_DIRS})
link_directories(${CMAKE_INSTALL_PREFIX}/lib/orocos/plugins ${CMAKE_INSTALL_PREFIX}/lib/orocos/types)
list(APPEND CMAKE_PREFIX_PATH ${OrocosRTT_PREFIX})

add_executable(test test.cpp)
target_link_libraries(test simple-typekit-${OROCOS_TARGET})
target_link_libraries(test ${OrocosRTT_LIBRARIES})
find_package( RTTPlugin COMPONENTS rtt-typekit rtt-marshalling)
target_link_libraries(test ${RTT_PLUGIN_rtt-marshalling_LIBRARY})
target_link_libraries(test ${RTT_PLUGIN_rtt-typekit_LIBRARY})
install(TARGETS test RUNTIME DESTINATION bin)
            EOT

            transports.each do |transport_name|
                cmake << "target_link_libraries(test simple-transport-#{transport_name}-${OROCOS_TARGET})\n"
{'fraction_non_alphanumeric': 0.10539746365604702, 'fraction_numerical': 0.0012372409526755336, 'mean_word_length': 4.10781990521327, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, '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': '3059114', 'n_tokens_mistral': 4349, 'n_tokens_neox': 4181, 'n_words': 601}