Skip to content

Commit

Permalink
improve stringification of elements in NumMatrix2D to follow best-pra…
Browse files Browse the repository at this point in the history
…ctices and be more compatible with the get_string vtables of other matrix types. Fix string tests for the type to match the new behavior
  • Loading branch information
Whiteknight committed Aug 18, 2010
1 parent ff04755 commit 6dfdf6d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
10 changes: 5 additions & 5 deletions src/pmc/nummatrix2d.pmc
Expand Up @@ -321,8 +321,7 @@ elements.

VTABLE STRING * get_string_keyed(PMC * key) {
const FLOATVAL f = VTABLE_get_number_keyed(INTERP, SELF, key);
STRING * const item = Parrot_sprintf_c(INTERP, "%f", f);
return item;
return Parrot_str_from_num(INTERP, f);
}

VTABLE PMC * get_pmc_keyed(PMC * key) {
Expand Down Expand Up @@ -375,8 +374,7 @@ Nth number. The array is arranged in memory row by row.

VTABLE STRING * get_string_keyed_int(INTVAL key) {
const FLOATVAL f = VTABLE_get_number_keyed_int(INTERP, SELF, key);
STRING * const item = Parrot_sprintf_c(INTERP, "%f", f);
return item;
return Parrot_str_from_num(INTERP, f);
}

VTABLE PMC * get_pmc_keyed_int(INTVAL key) {
Expand Down Expand Up @@ -444,6 +442,7 @@ Get a string representation of the matrix.
INTVAL i, j;
PMC * const builder = Parrot_pmc_new(INTERP, enum_class_StringBuilder);
STRING * const newline = Parrot_str_new(INTERP, "\n", 1);
STRING * const tab = Parrot_str_new(INTERP, "\t", 1);
FLOATVAL * const s = attrs->storage;
const INTVAL rows = attrs->rows;
const INTVAL cols = attrs->cols;
Expand All @@ -452,7 +451,8 @@ Get a string representation of the matrix.
for (i = 0; i < rows; ++i) {
for (j = 0; j < cols; ++j) {
const FLOATVAL f = ITEM_XY(s, flags, rows, cols, i, j);
STRING * const item = Parrot_sprintf_c(INTERP, "\t%f", f);
STRING * const item = Parrot_str_from_num(INTERP, f);
VTABLE_push_string(INTERP, builder, tab);
VTABLE_push_string(INTERP, builder, item);
}
VTABLE_push_string(INTERP, builder, newline);
Expand Down
9 changes: 4 additions & 5 deletions t/pmc/nummatrix2d.t
Expand Up @@ -35,10 +35,13 @@ method test_VTABLE_get_string() {
my $m := self.matrix2x2(1.0, 2.0,
3.0, 4.0);
my $s := pir::set__SP($m);
my $t := pir::sprintf__SSP("\t%f\t%f\n\t%f\t%f\n", [1.0, 2.0, 3.0, 4.0]);
my $t := pir::sprintf__SSP("\t%S\t%S\n\t%S\t%S\n", [1.0, 2.0, 3.0, 4.0]);
assert_equal($s, $t, "cannot get string");
}

# TODO: Tests for get/set_pmc to prove that we get a Float from it
# $P1 = $P0[0]
# assert_instance_of($P1, "Float", "got Number PMC from linear index")

# Addition Tests

Expand All @@ -59,7 +62,6 @@ method test_VTABLE_add_NUMMATRIX2D_SIZEFAIL() {
});
}


method test_VTABLE_i_add_NUMMATRIX2D() {
my $m := self.matrix2x2(1.0, 3.0, 2.0, 4.0);
my $n := self.matrix2x2(5.0, 7.0, 6.0, 8.0);
Expand All @@ -73,7 +75,6 @@ method test_VTABLE_i_add_NUMMATRIX2D() {
}
}
# Subtraction Tests
method test_VTABLE_subtract_NUMMATRIX2D() {
Expand Down Expand Up @@ -107,7 +108,6 @@ method test_VTABLE_i_subtract_NUMMATRIX2D() {
}
}
# Multiplication Tests
method test_VTABLE_multiply_NUMMATRIX2D() {
Expand Down Expand Up @@ -158,7 +158,6 @@ method test_VTABLE_i_multiply_NUMMATRIX2D() {
}
}
# Block Get/Set method tests
method test_METHOD_set_block() {
Expand Down

0 comments on commit 6dfdf6d

Please sign in to comment.