File: va_list_spec.cr

package info (click to toggle)
crystal 1.14.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 24,384 kB
  • sloc: javascript: 6,400; sh: 695; makefile: 269; ansic: 121; python: 105; cpp: 77; xml: 32
file content (42 lines) | stat: -rw-r--r-- 961 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
{% skip_file if flag?(:win32) || flag?(:aarch64) %}

require "./spec_helper"

describe VaList do
  it "works with C code", tags: %w[slow] do
    compile_and_run_source_with_c(
      %(
          #include <stdarg.h>
          extern int foo_f(int,...);
          int foo() {
            return foo_f(3,1,2,3);
          }

          int read_arg(va_list *ap) {
            return va_arg(*ap, int);
          }
        ),
      %(
        lib LibFoo
          fun foo : LibC::Int
          fun read_arg(ap : LibC::VaList*) : LibC::Int
        end

        fun foo_f(count : LibC::Int, ...) : LibC::Int
          sum = 0
          VaList.open do |list|
            ap = list.to_unsafe
            count.times do |i|
              sum += LibFoo.read_arg(pointerof(ap))
            end
          end
          sum
        end

        puts LibFoo.foo
      )) do |status, output|
      status.success?.should be_true
      output.to_i.should eq(6)
    end
  end
end