mirror of
https://github.com/tailix/libkernaux.git
synced 2025-04-14 17:32:55 -04:00
Conditional compilation
This commit is contained in:
parent
d1105f1cc6
commit
edf40ddb83
4 changed files with 94 additions and 76 deletions
|
@ -2,9 +2,9 @@
|
|||
|
||||
require 'mkmf'
|
||||
|
||||
have_library 'kernaux'
|
||||
raise 'libkernaux not found' unless have_library 'kernaux'
|
||||
|
||||
have_func 'kernaux_utoa10'
|
||||
have_func 'kernaux_itoa10'
|
||||
|
||||
create_makefile 'kernaux/default'
|
||||
raise 'can\'t create Makefile' unless create_makefile 'kernaux/default'
|
||||
|
|
|
@ -5,16 +5,25 @@
|
|||
|
||||
static VALUE rb_KernAux = Qnil;
|
||||
|
||||
#ifdef HAVE_KERNAUX_UTOA10
|
||||
static VALUE rb_KernAux_utoa10(VALUE self, VALUE number);
|
||||
#endif
|
||||
#ifdef HAVE_KERNAUX_ITOA10
|
||||
static VALUE rb_KernAux_itoa10(VALUE self, VALUE number);
|
||||
#endif
|
||||
|
||||
void Init_default()
|
||||
{
|
||||
rb_KernAux = rb_define_module("KernAux");
|
||||
#ifdef HAVE_KERNAUX_UTOA10
|
||||
rb_define_singleton_method(rb_KernAux, "utoa10", rb_KernAux_utoa10, 1);
|
||||
#endif
|
||||
#ifdef HAVE_KERNAUX_ITOA10
|
||||
rb_define_singleton_method(rb_KernAux, "itoa10", rb_KernAux_itoa10, 1);
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef HAVE_KERNAUX_UTOA10
|
||||
VALUE rb_KernAux_utoa10(
|
||||
const VALUE self_rb __attribute__((unused)),
|
||||
const VALUE number_rb
|
||||
|
@ -27,7 +36,9 @@ VALUE rb_KernAux_utoa10(
|
|||
kernaux_utoa10(NUM2ULL(number_rb), buffer);
|
||||
return rb_funcall(rb_str_new2(buffer), rb_intern("freeze"), 0);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_KERNAUX_ITOA10
|
||||
VALUE rb_KernAux_itoa10(
|
||||
const VALUE self_rb __attribute__((unused)),
|
||||
const VALUE number_rb
|
||||
|
@ -37,3 +48,4 @@ VALUE rb_KernAux_itoa10(
|
|||
kernaux_itoa10(NUM2LL(number_rb), buffer);
|
||||
return rb_funcall(rb_str_new2(buffer), rb_intern("freeze"), 0);
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -9,100 +9,104 @@ RSpec.describe KernAux do
|
|||
end
|
||||
end
|
||||
|
||||
describe '.utoa10' do
|
||||
subject(:utoa10) { described_class.utoa10 number }
|
||||
if described_class.singleton_class.method_defined? :utoa10
|
||||
describe '.utoa10' do
|
||||
subject(:utoa10) { described_class.utoa10 number }
|
||||
|
||||
let(:number) { rand 0..(2**64 - 1) }
|
||||
|
||||
it { is_expected.to be_instance_of String }
|
||||
it { is_expected.to be_frozen }
|
||||
it { is_expected.to eq number.to_s }
|
||||
|
||||
context 'when number is 0' do
|
||||
let(:number) { 0 }
|
||||
|
||||
it { is_expected.to eq '0' }
|
||||
end
|
||||
|
||||
context 'when number is max uint64_t' do
|
||||
let(:number) { 2**64 - 1 }
|
||||
let(:number) { rand 0..(2**64 - 1) }
|
||||
|
||||
it { is_expected.to be_instance_of String }
|
||||
it { is_expected.to be_frozen }
|
||||
it { is_expected.to eq number.to_s }
|
||||
end
|
||||
|
||||
context 'when number is -1' do
|
||||
let(:number) { -1 }
|
||||
context 'when number is 0' do
|
||||
let(:number) { 0 }
|
||||
|
||||
specify do
|
||||
expect { utoa10 }.to \
|
||||
raise_error RangeError, 'can\'t convert negative number to uint64_t'
|
||||
it { is_expected.to eq '0' }
|
||||
end
|
||||
end
|
||||
|
||||
context 'when number is greater than max uint64_t' do
|
||||
let(:number) { 2**64 }
|
||||
context 'when number is max uint64_t' do
|
||||
let(:number) { 2**64 - 1 }
|
||||
|
||||
specify do
|
||||
expect { utoa10 }.to raise_error \
|
||||
RangeError, 'bignum too big to convert into `unsigned long long\''
|
||||
it { is_expected.to eq number.to_s }
|
||||
end
|
||||
|
||||
context 'when number is -1' do
|
||||
let(:number) { -1 }
|
||||
|
||||
specify do
|
||||
expect { utoa10 }.to \
|
||||
raise_error RangeError, 'can\'t convert negative number to uint64_t'
|
||||
end
|
||||
end
|
||||
|
||||
context 'when number is greater than max uint64_t' do
|
||||
let(:number) { 2**64 }
|
||||
|
||||
specify do
|
||||
expect { utoa10 }.to raise_error \
|
||||
RangeError, 'bignum too big to convert into `unsigned long long\''
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '.itoa10' do
|
||||
subject(:itoa10) { described_class.itoa10 number }
|
||||
if described_class.singleton_class.method_defined? :itoa10
|
||||
describe '.itoa10' do
|
||||
subject(:itoa10) { described_class.itoa10 number }
|
||||
|
||||
let(:number) { rand((-2**63)..(2**63 - 1)) }
|
||||
|
||||
it { is_expected.to be_instance_of String }
|
||||
it { is_expected.to be_frozen }
|
||||
it { is_expected.to eq number.to_s }
|
||||
|
||||
context 'when number is 0' do
|
||||
let(:number) { 0 }
|
||||
|
||||
it { is_expected.to eq '0' }
|
||||
end
|
||||
|
||||
context 'when number is 1' do
|
||||
let(:number) { 1 }
|
||||
|
||||
it { is_expected.to eq '1' }
|
||||
end
|
||||
|
||||
context 'when number is -1' do
|
||||
let(:number) { -1 }
|
||||
|
||||
it { is_expected.to eq '-1' }
|
||||
end
|
||||
|
||||
context 'when number is min int64_t' do
|
||||
let(:number) { -2**63 }
|
||||
let(:number) { rand((-2**63)..(2**63 - 1)) }
|
||||
|
||||
it { is_expected.to be_instance_of String }
|
||||
it { is_expected.to be_frozen }
|
||||
it { is_expected.to eq number.to_s }
|
||||
end
|
||||
|
||||
context 'when number is max int64_t' do
|
||||
let(:number) { 2**63 - 1 }
|
||||
context 'when number is 0' do
|
||||
let(:number) { 0 }
|
||||
|
||||
it { is_expected.to eq number.to_s }
|
||||
end
|
||||
|
||||
context 'when number is lesser than min uint64_t' do
|
||||
let(:number) { -2**63 - 1 }
|
||||
|
||||
specify do
|
||||
expect { itoa10 }.to \
|
||||
raise_error RangeError, 'bignum too big to convert into `long long\''
|
||||
it { is_expected.to eq '0' }
|
||||
end
|
||||
end
|
||||
|
||||
context 'when number is greater than max uint64_t' do
|
||||
let(:number) { 2**63 }
|
||||
context 'when number is 1' do
|
||||
let(:number) { 1 }
|
||||
|
||||
specify do
|
||||
expect { itoa10 }.to \
|
||||
raise_error RangeError, 'bignum too big to convert into `long long\''
|
||||
it { is_expected.to eq '1' }
|
||||
end
|
||||
|
||||
context 'when number is -1' do
|
||||
let(:number) { -1 }
|
||||
|
||||
it { is_expected.to eq '-1' }
|
||||
end
|
||||
|
||||
context 'when number is min int64_t' do
|
||||
let(:number) { -2**63 }
|
||||
|
||||
it { is_expected.to eq number.to_s }
|
||||
end
|
||||
|
||||
context 'when number is max int64_t' do
|
||||
let(:number) { 2**63 - 1 }
|
||||
|
||||
it { is_expected.to eq number.to_s }
|
||||
end
|
||||
|
||||
context 'when number is lesser than min uint64_t' do
|
||||
let(:number) { -2**63 - 1 }
|
||||
|
||||
specify do
|
||||
expect { itoa10 }.to raise_error \
|
||||
RangeError, 'bignum too big to convert into `long long\''
|
||||
end
|
||||
end
|
||||
|
||||
context 'when number is greater than max uint64_t' do
|
||||
let(:number) { 2**63 }
|
||||
|
||||
specify do
|
||||
expect { itoa10 }.to raise_error \
|
||||
RangeError, 'bignum too big to convert into `long long\''
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -17,12 +17,14 @@
|
|||
|
||||
#include <stddef.h>
|
||||
|
||||
#ifdef WITH_PRINTF
|
||||
static void kernaux_console_printf_putc(
|
||||
const char c,
|
||||
void *const arg __attribute__((unused))
|
||||
) {
|
||||
kernaux_console_putc(c);
|
||||
}
|
||||
#endif
|
||||
|
||||
void kernaux_console_putc(const char c __attribute__((unused)))
|
||||
{
|
||||
|
|
Loading…
Add table
Reference in a new issue