From 6dfdf6d906e4058d3540805dc6f5cccc47077348 Mon Sep 17 00:00:00 2001 From: Whiteknight Date: Wed, 18 Aug 2010 13:32:27 -0400 Subject: [PATCH] improve stringification of elements in NumMatrix2D to follow best-practices and be more compatible with the get_string vtables of other matrix types. Fix string tests for the type to match the new behavior --- src/pmc/nummatrix2d.pmc | 10 +++++----- t/pmc/nummatrix2d.t | 9 ++++----- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/pmc/nummatrix2d.pmc b/src/pmc/nummatrix2d.pmc index c688fe3..00f59fb 100644 --- a/src/pmc/nummatrix2d.pmc +++ b/src/pmc/nummatrix2d.pmc @@ -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) { @@ -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) { @@ -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; @@ -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); diff --git a/t/pmc/nummatrix2d.t b/t/pmc/nummatrix2d.t index 98cd58d..853e253 100644 --- a/t/pmc/nummatrix2d.t +++ b/t/pmc/nummatrix2d.t @@ -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 @@ -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); @@ -73,7 +75,6 @@ method test_VTABLE_i_add_NUMMATRIX2D() { } } - # Subtraction Tests method test_VTABLE_subtract_NUMMATRIX2D() { @@ -107,7 +108,6 @@ method test_VTABLE_i_subtract_NUMMATRIX2D() { } } - # Multiplication Tests method test_VTABLE_multiply_NUMMATRIX2D() { @@ -158,7 +158,6 @@ method test_VTABLE_i_multiply_NUMMATRIX2D() { } } - # Block Get/Set method tests method test_METHOD_set_block() {