Ruby: add original tests for KernAux.sprintf

This commit is contained in:
Alex Kotov 2022-01-22 06:13:38 +05:00
parent 580b7698c8
commit f7aff4bf88
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
3 changed files with 53 additions and 4 deletions

View File

@ -13,6 +13,8 @@ Layout/AccessModifierIndentation:
Layout/LineLength:
Max: 80
Exclude:
- 'spec/lib/kernaux/sprintf_spec.rb'
Lint/AmbiguousOperatorPrecedence:
Enabled: false
@ -30,6 +32,7 @@ RSpec/ContextWording:
Prefixes:
- 'and'
- 'for'
- 'using'
- 'when'
- 'with'
- 'without'

View File

@ -189,11 +189,12 @@ VALUE rb_KernAux_snprintf1(
if (*(fmt++) == '%') rb_raise(rb_eArgError, "invalid format");
}
bool use_dbl = false;
double dbl;
union {
const char *str;
long long ll;
unsigned long long ull;
double dbl;
char chr;
} __attribute__((packed)) arg = { .str = "" };
@ -210,9 +211,9 @@ VALUE rb_KernAux_snprintf1(
c == 'e' || c == 'E' ||
c == 'g' || c == 'G')
{
// FIXME: this doesn't work
RB_FLOAT_TYPE_P(arg_rb);
arg.dbl = NUM2DBL(arg_rb);
use_dbl = true;
dbl = NUM2DBL(arg_rb);
} else if (c == 'c') {
Check_Type(arg_rb, T_STRING);
arg.chr = *StringValuePtr(arg_rb);
@ -224,7 +225,9 @@ VALUE rb_KernAux_snprintf1(
char *const str = malloc(size);
if (!str) rb_raise(rb_eNoMemError, "snprintf1 buffer malloc");
const int slen = kernaux_snprintf(str, size, format, arg, "", "", "");
const int slen = use_dbl
? kernaux_snprintf(str, size, format, dbl)
: kernaux_snprintf(str, size, format, arg, "", "", "");
const VALUE output_rb =
rb_funcall(rb_str_new2(str), rb_intern_freeze, 0);
free(str);

View File

@ -1,5 +1,8 @@
# frozen_string_literal: true
# Copyright (c) 2014-2019 Marco Paland <info@paland.com>
# Author: Marco Paland (info@paland.com) PALANDesign Hannover, Germany
require 'spec_helper'
RSpec.describe KernAux, '.sprintf' do
@ -8,4 +11,44 @@ RSpec.describe KernAux, '.sprintf' do
it { is_expected.to be_instance_of String }
it { is_expected.to be_frozen }
it { is_expected.to eq 'Hello, World!' }
context 'using original tests' do
[
[' 4232', ['% d', 4232]],
['This is a test of 12EF', ['This is a test of %X', 0x12EF]],
['-1000', ['%d', -1000]],
['-1', ['%d', -1]],
['2345', ['%d', 2345]],
['3 -1000 test', ['%d ', 3], ['%d ', -1000], ['%s', 'test']],
['3 -1000 test', ['%d', 3], [' %d', -1000], [' %s', 'test']],
['3 -1000 test', ['%d', 3], ' ', ['%d', -1000], ' ', ['%s', 'test']],
# space flag
[' 42', ['% d', 42]],
['-42', ['% d', -42]],
[' 42', ['% 5d', 42]],
[' -42', ['% 5d', -42]],
[' 42', ['% 15d', 42]],
[' -42', ['% 15d', -42]],
[' -42.987', ['% 15.3f', -42.987]],
[' 42.987', ['% 15.3f', 42.987]],
['Hello testing', ['% s', 'Hello testing']],
[' 1024', ['% d', 1024]],
['-1024', ['% d', -1024]],
[' 1024', ['% i', 1024]],
['-1024', ['% i', -1024]],
['1024', ['% u', 1024]],
['4294966272', ['% u', 4_294_966_272]],
['777', ['% o', 511]],
['37777777001', ['% o', 4_294_966_785]],
['1234abcd', ['% x', 305_441_741]],
['edcb5433', ['% x', 3_989_525_555]],
['1234ABCD', ['% X', 305_441_741]],
['EDCB5433', ['% X', 3_989_525_555]],
['x', ['% c', 'x']],
].each do |expected, *args|
specify do
expect(described_class.sprintf(*args)).to eq expected
end
end
end
end