Top Level Namespace

Defined Under Namespace

Modules: AutoC Classes: CompositeValue, Docs, DocsValue, GenericValue

Constant Summary collapse

A =
DocsValue.new(:A, 'Generic value type')
B =
DocsValue.new(:B, 'Generic value type')
T =
DocsValue.new(:T, 'Generic value type')
K =
DocsValue.new(:K, 'Generic hashable value type')
Value =
GenericValue.new(:Value, visibility: :public)
GlassRecord =
AutoC::Record.new(:GlassRecord, { i: :int }, profile: :glassbox, visibility: :public)
CString =
type_test(AutoC::CString,  :CString) do

  ###

  setup %{
    #{self} s;
  }

  cleanup %{
    #{destroy.(:s)};
  }

  test :create_empty, %{
    #{custom_create.(:s, %{""})};
    TEST_EQUAL( #{size.(:s)}, 0 );
    TEST_TRUE( #{empty}(s) );
  }

  test :create_non_empty, %{
    #{custom_create.(:s, %{"hello"})};
    TEST_EQUAL( #{size.(:s)}, 5 );
  }

  test :format_empty, %{
    TEST_TRUE( #{create_format.(:s, %{""})} );
    TEST_TRUE( #{empty}(s) );
  }

  test :format_empty_params, %{
    TEST_TRUE( #{create_format.(:s, %{"hello"})} );
    TEST_TRUE( #{equal}(s, "hello") );
    TEST_FALSE( #{empty}(s) );
  }
  
  test :format_int, %{
    TEST_TRUE( #{create_format.(:s, %{"hello%d"}, -3)} );
    TEST_TRUE( #{equal}("hello-3", s) );
  }

  test :equal_cstr, %{
    #{custom_create.(:s, %{"hello"})};
    TEST_TRUE( #{equal.(:s, %{"hello"})} );
  }

  test :compare_cstr, %{
    #{custom_create.(:s, %{"hello"})};
    TEST_FALSE( #{equal.(%{"hello1"}, :s)} );
    TEST_FALSE( #{equal.(:s, %{"hello1"})} );
    TEST_TRUE( #{compare.(:s, %{"hello1"})} < 0 );
    TEST_TRUE( #{compare.(%{"hello1"}, :s)} > 0 );
  }

  ###

  setup %{
    #{self} s1, s2;
  }

  cleanup %{
    #{destroy.(:s1)};
    #{destroy.(:s2)};
  }

  test :compare_equal, %{
    #{custom_create.(:s1, %{"hello"})};
    #{custom_create.(:s2, %{"hello"})};
    TEST_TRUE( #{equal.(:s1, :s2)} );
    TEST_EQUAL( #{compare.(:s1, :s2)}, 0 );
    TEST_EQUAL( #{hash_code.(:s1)}, #{hash_code.(:s2)} );
  }

  test :compare_non_equal, %{
    #{custom_create.(:s1, %{"hello1"})};
    #{custom_create.(:s2, %{"hello2"})};
    TEST_FALSE( #{equal.(:s1, :s2)} );
    TEST_FALSE( #{equal.(:s2, :s1)} );
    TEST_TRUE( #{compare.(:s1, :s2)} < 0 );
    TEST_TRUE( #{compare.(:s2, :s1)} > 0 );
  }

  ###

  setup %{
    #{self} s;
    #{custom_create.(:s, %{"hello"})};
  }

  cleanup %{
    #{destroy.(:s)};
  }

  test :char_access, %{
    TEST_EQUAL( #{get.(:s, 0)}, 'h' );
    TEST_EQUAL( *#{view}(s, 4), 'o' );
  }

  test :range_access, %{
    #{range} r = #{range.new.(:s)};
    TEST_EQUAL( #{size}(s), 5 );
    TEST_EQUAL( #{range.size}(&r), #{size}(s) );
    TEST_EQUAL( #{range.take_front}(&r), 'h' );
    TEST_EQUAL( *#{range.view_back}(&r), 'o' );
    #{range.pop_front}(&r);
    TEST_EQUAL( *#{range.view_front}(&r), 'e' );
    TEST_EQUAL( #{range.size}(&r), #{size}(s)-1 );
    #{range.pop_back}(&r);
    TEST_EQUAL( #{range.take_back}(&r), 'l' );
    TEST_EQUAL( #{range.size}(&r), #{size}(s)-2 );
  }
  
end
IntVector =
type_test(AutoC::Vector, :IntVector, :int) do

  ###

  setup %{
    #{self} t;
  }

  cleanup %{
    #{destroy}(&t);
  }

  test :create, %{
    #{default_create.(:t)};
    TEST_EQUAL( #{size}(&t), 0 );
  }

  test :create_size, %{
    #{custom_create.(:t, 100)};
    TEST_EQUAL( #{size}(&t), 100 );
  }

  ###

  setup %{
    #{self} t1, t2;
    int i, c = 3;
    #{custom_create.(:t1, :c)};
    #{custom_create.(:t2, :c)};
    for(i = 0; i < c; ++i) {
      #{set}(&t1, i, i);
      #{set}(&t2, i, i);
      /* 0,1,2 */
    }
  }

  cleanup %{
    #{destroy}(&t1);
    #{destroy}(&t2);
  }

  test :equal_identity, %{
    TEST_TRUE( #{equal.(:t1, :t1)} );
    TEST_TRUE( #{equal.(:t2, :t2)} );
  }

  test :equal, %{
    TEST_TRUE( #{equal.(:t1, :t2)} );
  }

  test :not_equal, %{
    #{set}(&t1, 1, -1);
    TEST_FALSE( #{equal.(:t1, :t2)} );
  }

  test :sort_resort, %{
    #{sort}(&t2, -1);
    TEST_EQUAL( #{get}(&t2, 0), 2);
    TEST_FALSE( #{equal.(:t1, :t2)} );
    #{sort}(&t2, +1);
    TEST_EQUAL( #{get}(&t2, 0), 0);
    TEST_TRUE( #{equal.(:t1, :t2)} );
  }

  test :sort_descend, %{
    #{sort}(&t2, -1);
    /* 2,1,0 */
    TEST_FALSE( #{equal.(:t1, :t2)} );
    TEST_EQUAL( #{get}(&t2, 0), 2 );
    TEST_EQUAL( #{get}(&t2, 1), 1 );
  }

  test :sort_ascend, %{
    #{sort}(&t2, +1);
    /* 0,1,2 */
    TEST_TRUE( #{equal.(:t1, :t2)} );
    TEST_EQUAL( #{get}(&t2, 0), 0 );
    TEST_EQUAL( #{get}(&t2, 1), 1 );
  }

end

Instance Method Summary collapse

Instance Method Details

#cmakelists_txt(project) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/autoc/scaffold/project.rb', line 20

def cmakelists_txt(project)
<<END
cmake_minimum_required(VERSION 3.15)

project(#{project})

set(AUTOC_MODULE_NAME _${PROJECT_NAME})
set(AUTOC_MODULE_SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}.rb)

list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/cmake)

include(AutoC)

add_autoc_module(
  ${AUTOC_MODULE_NAME}
  DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  MAIN_DEPENDENCY ${AUTOC_MODULE_SOURCE}
  COMMAND ${Ruby_EXECUTABLE} ${AUTOC_MODULE_SOURCE}
)

add_executable(${PROJECT_NAME} ${PROJECT_NAME}.c)
target_link_libraries(${PROJECT_NAME} ${AUTOC_MODULE_NAME})
END
end

#docsObject

Generate generated interface reference header to processable with Doxygen



6
# File 'lib/autoc/scaffold.rb', line 6

def docs = require_relative 'scaffold/docs'

#projectObject

Generate skeleton project



12
# File 'lib/autoc/scaffold.rb', line 12

def project = require_relative 'scaffold/project'

#project_c(project) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/autoc/scaffold/project.rb', line 2

def project_c(project)
<<END
#include "_#{project}_auto.h"

#include <stdio.h>

int main(int argc, char **argv) {
  CString msg;
  CStringCreateFormat(&msg, "Hello %s!\\n", "#{project}");
  printf(msg);
  CStringDestroy(&msg);
  return 0;
}
END
end

#project_rb(project) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/autoc/scaffold/project.rb', line 47

def project_rb(project)
<<END
require 'autoc/module'
require 'autoc/cstring'

m = AutoC::Module.render(:_#{project}) do |m|
  m << AutoC::CString.new
end

require 'autoc/cmake'

AutoC::CMake.render(m)
END
end

#testsObject

Generated test suite



9
# File 'lib/autoc/scaffold.rb', line 9

def tests = require_relative 'scaffold/tests'

#type_test(cls, *opts, **kws, &code) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/autoc/scaffold/tests.rb', line 28

def type_test(cls, *opts, **kws, &code)
  t = Class.new(cls) do
    attr_reader :tests
    def initialize(*args, **kws)
      super(*args, visibility: :public, **kws)
      @tests = []
      @test_names = []
      dependencies << $common
    end
    def setup(code = nil)
      @setup_code = code
    end
    def cleanup(code = nil)
      @cleanup_code = code
    end
    def test(name, code)
      s = name.to_s
      @test_names << [name, func_name = identifier("test_#{name}")]
      @tests << %{
        void #{func_name}(void) {
          #{@setup_code}
          #{code}
          #{@cleanup_code}
        } /* #{func_name} */ 
      }
    end
    def render_forward_declarations(stream)
      super
      stream << "void #{run_tests}(void);"
    end
    def render_implementation(stream)
      super
      @tests.each { |f| stream << f }
      stream << "void #{run_tests}(void) {"
      stream << %{
        fprintf(stdout, "* %s\\n", "#{type}");
        fflush(stdout);
      }
      @test_names.each { |name, func_name| stream << %$run_test("#{name}", #{func_name});$ }
      stream << %$fputs("\\n", stdout); fflush(stdout);}$
    end
    def write_test_calls(stream)
      stream << "#{run_tests}();"
    end
  end.new(*opts, **kws)
  $tests << t
  t.instance_eval(&code)
  t
end