Skip to content

Commit cff9a88

Browse files
authored
Merge pull request #53 from isuruf/fixes
Fixes
2 parents a7b1d13 + ca25263 commit cff9a88

File tree

11 files changed

+61
-30
lines changed

11 files changed

+61
-30
lines changed

.travis.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ addons:
1313
- libmpc-dev
1414
- binutils-dev
1515
- g++-4.7
16+
1617
rvm:
1718
- 2.0
1819
- 2.1
@@ -23,6 +24,15 @@ matrix:
2324
exclude:
2425
- os: osx
2526
include:
27+
- os: linux
28+
rvm: 2.0
29+
env: BUILD_TYPE="Debug" WITH_BFD="yes" WITH_MPC="yes"
30+
- os: linux
31+
rvm: 2.3.0
32+
env: BUILD_TYPE="Debug" WITH_BFD="yes" WITH_MPC="yes"
33+
- os: linux
34+
rvm: 2.3.0
35+
env: BUILD_TYPE="Debug" WITH_BFD="yes" WITH_MPFR="yes"
2636
- os: osx
2737
rvm: 2.0
2838
allow_failures:
@@ -52,6 +62,8 @@ install:
5262
script:
5363
# Build ruby gem
5464
- gem build symengine.gemspec
65+
# Convert all warnings to errors
66+
- export CFLAGS="-Werror"
5567
# Install ruby gem
5668
- gem install symengine-0.1.0.gem --install-dir $HOME --verbose -- -DSymEngine_DIR=$our_install_dir
5769

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ set(CMAKE_PREFIX_PATH ${SymEngine_DIR} ${CMAKE_PREFIX_PATH})
77
find_package(SymEngine 0.1.0 REQUIRED CONFIG
88
PATH_SUFFIXES lib/cmake/symengine CMake/)
99
set(CMAKE_BUILD_TYPE ${SYMENGINE_BUILD_TYPE})
10-
set(CMAKE_CXX_FLAGS_RELEASE ${SYMENGINE_CXX_FLAGS_RELEASE})
11-
set(CMAKE_CXX_FLAGS_DEBUG ${SYMENGINE_CXX_FLAGS_DEBUG})
10+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
11+
message("SymEngine directory: ${SymEngine_DIR}")
1212
include_directories(${SYMENGINE_INCLUDE_DIRS})
1313

1414
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/")

ext/symengine/ruby_basic.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,15 @@ VALUE cbasic_diff(VALUE self, VALUE operand2) {
9090
}
9191

9292
VALUE cbasic_eq(VALUE self, VALUE operand2) {
93+
9394
basic_struct *this;
9495

9596
basic cbasic_operand2;
9697
basic_new_stack(cbasic_operand2);
9798
Data_Get_Struct(self, basic_struct, this);
98-
sympify(operand2, cbasic_operand2);
99+
100+
VALUE ret = check_sympify(operand2, cbasic_operand2);
101+
if (ret == Qfalse) return Qfalse;
99102

100103
VALUE ret_val = basic_eq(this, cbasic_operand2) ? Qtrue : Qfalse;
101104
basic_free_stack(cbasic_operand2);
@@ -109,7 +112,9 @@ VALUE cbasic_neq(VALUE self, VALUE operand2) {
109112
basic cbasic_operand2;
110113
basic_new_stack(cbasic_operand2);
111114
Data_Get_Struct(self, basic_struct, this);
112-
sympify(operand2, cbasic_operand2);
115+
116+
VALUE ret = check_sympify(operand2, cbasic_operand2);
117+
if (ret == Qfalse) return Qtrue;
113118

114119
VALUE ret_val = basic_neq(this, cbasic_operand2) ? Qtrue : Qfalse;
115120
basic_free_stack(cbasic_operand2);
@@ -122,7 +127,7 @@ VALUE cbasic_neg(VALUE self){
122127
}
123128

124129
VALUE cbasic_get_args(VALUE self) {
125-
basic_struct *this, *iterator_basic;
130+
basic_struct *this;
126131
CVecBasic *args = vecbasic_new();
127132
int size = 0;
128133

@@ -132,7 +137,7 @@ VALUE cbasic_get_args(VALUE self) {
132137
size = vecbasic_size(args);
133138
VALUE ruby_array = rb_ary_new2(size);
134139
int i = 0;
135-
VALUE temp = NULL;
140+
VALUE temp;
136141
for(i = 0; i < size; i++) {
137142
basic_struct *temp_basic = basic_new_heap();
138143
vecbasic_get(args, i, temp_basic);
@@ -144,7 +149,7 @@ VALUE cbasic_get_args(VALUE self) {
144149
}
145150

146151
VALUE cbasic_free_symbols(VALUE self) {
147-
basic_struct *this, *iterator_basic;
152+
basic_struct *this;
148153
CSetBasic *symbols = setbasic_new();
149154
int size = 0;
150155

@@ -154,7 +159,7 @@ VALUE cbasic_free_symbols(VALUE self) {
154159
size = setbasic_size(symbols);
155160
VALUE ruby_array = rb_ary_new2(size);
156161
int i = 0;
157-
VALUE temp = NULL;
162+
VALUE temp;
158163
for(i = 0; i < size; i++) {
159164
basic_struct *temp_basic = basic_new_heap();
160165
setbasic_get(symbols, i, temp_basic);

ext/symengine/ruby_real_mpfr.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ VALUE crealmpfr_init(VALUE self, VALUE num_value, VALUE prec_value)
2020
real_mpfr_set_str(cresult, c, prec);
2121
break;
2222
case T_DATA:
23-
c = rb_obj_classname(num_value);
24-
if (strcmp(c, "BigDecimal") == 0) {
23+
if (strcmp(rb_obj_classname(num_value), "BigDecimal") == 0) {
2524
c = RSTRING_PTR(rb_funcall(num_value, rb_intern("to_s"), 1, rb_str_new2("F")));
2625
real_mpfr_set_str(cresult, c, prec);
2726
break;

ext/symengine/ruby_symbol.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
#include "ruby_symbol.h"
22

33
VALUE csymbol_init(VALUE self, VALUE name_or_id) {
4-
char *str_ptr;
4+
const char *str_ptr;
55

66
switch (TYPE(name_or_id)) {
7-
case T_STRING:
8-
str_ptr = StringValueCStr(name_or_id);
9-
break;
10-
case T_SYMBOL:
11-
str_ptr = rb_id2name(rb_to_id(name_or_id));
12-
break;
13-
default:
14-
rb_raise(rb_eTypeError, "wrong argument type %s (expected Symbol or String)", rb_obj_classname(name_or_id));
7+
case T_STRING:
8+
str_ptr = StringValueCStr(name_or_id);
9+
break;
10+
case T_SYMBOL:
11+
str_ptr = rb_id2name(rb_to_id(name_or_id));
12+
break;
13+
default:
14+
rb_raise(rb_eTypeError, "wrong argument type %s (expected Symbol or String)", rb_obj_classname(name_or_id));
1515
}
1616

1717
basic_struct *this;

ext/symengine/symengine.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,5 @@ void Init_symengine() {
207207
rb_define_module_function(m_symengine, "lucas", cntheory_lucas, 1);
208208
rb_define_module_function(m_symengine, "binomial", cntheory_binomial, 2);
209209

210+
symengine_print_stack_on_segfault();
210211
}

ext/symengine/symengine.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#ifndef SYMENGINE_H_
22
#define SYMENGINE_H_
33

4-
#include "ruby.h"
4+
#include <ruby/ruby.h>
5+
#include <ruby/intern.h>
56

67
//variable name for a module starts with m
78
VALUE m_symengine;

ext/symengine/symengine_utils.c

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
#include "symengine_utils.h"
22
#include "symengine.h"
33

4-
void sympify(VALUE operand2, basic_struct *cbasic_operand2) {
4+
VALUE check_sympify(VALUE operand2, basic_struct *cbasic_operand2) {
55

66
basic_struct *temp;
7-
VALUE new_operand2;
87
VALUE a, b;
98
double f;
10-
char *c;
119

1210
switch(TYPE(operand2)) {
1311
case T_FIXNUM:
@@ -60,18 +58,29 @@ void sympify(VALUE operand2, basic_struct *cbasic_operand2) {
6058
break;
6159

6260
case T_DATA:
63-
c = rb_obj_classname(operand2);
61+
if (rb_obj_is_kind_of(operand2, c_basic)) {
62+
Data_Get_Struct(operand2, basic_struct, temp);
63+
basic_assign(cbasic_operand2, temp);
64+
break;
65+
}
6466
#ifdef HAVE_SYMENGINE_MPFR
65-
if (strcmp(c, "BigDecimal") == 0) {
67+
if (strcmp(rb_obj_classname(operand2), "BigDecimal") == 0) {
68+
const char *c;
6669
c = RSTRING_PTR( rb_funcall(operand2, rb_intern("to_s"), 1, rb_str_new2("F")) );
6770
real_mpfr_set_str(cbasic_operand2, c, 200);
6871
break;
6972
}
7073
#endif //HAVE_SYMENGINE_MPFR
74+
default:
75+
return Qfalse;
76+
}
77+
return Qtrue;
78+
}
7179

72-
Data_Get_Struct(operand2, basic_struct, temp);
73-
basic_assign(cbasic_operand2, temp);
74-
break;
80+
void sympify(VALUE operand2, basic_struct *cbasic_operand2) {
81+
VALUE ret = check_sympify(operand2, cbasic_operand2);
82+
if (ret == Qfalse) {
83+
rb_raise(rb_eTypeError, "%s can't be coerced into SymEngine::Basic", rb_obj_classname(operand2));
7584
}
7685
}
7786

ext/symengine/symengine_utils.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77

88
VALUE rb_cBigDecimal;
99

10-
//Returns the pointer wrapped inside the Ruby VALUE
10+
//Coerces operand2 into a SymEngine object
1111
void sympify(VALUE operand2, basic_struct *cbasic_operand2);
12+
//Coerces operand2 into a SymEngine object and returns whether successful
13+
VALUE check_sympify(VALUE operand2, basic_struct *cbasic_operand2);
1214
//Returns the pointer wrapped inside the Ruby Fixnum or Bignum
1315
void get_symintfromval(VALUE operand2, basic_struct *cbasic_operand2);
1416
//Returns the Ruby class of the corresponding basic_struct pointer

spec/basic_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
subject { x * y }
6161
it { is_expected.to eq sym('x')*sym('y') }
6262
it { is_expected.not_to eq sym('x')*sym('z') }
63+
it { is_expected.not_to eq "asd" }
6364
end
6465

6566
it 'simplifies' do
@@ -90,6 +91,7 @@
9091

9192
it 'raises on wrong args' do
9293
expect { 'x' * x }.to raise_error(TypeError)
94+
expect { x * 'x' }.to raise_error(TypeError)
9395
end
9496
end
9597

0 commit comments

Comments
 (0)