tests and bugfixes for splice and shift

This commit is contained in:
Ari Lazier 2015-08-28 15:11:50 -07:00
parent ddbc6eb4c9
commit fbf2399140
2 changed files with 80 additions and 12 deletions

View File

@ -24,12 +24,10 @@
using namespace realm; using namespace realm;
size_t ObjectArray::size() { size_t ObjectArray::size() {
verify_attached();
return link_view->size(); return link_view->size();
} }
Row ObjectArray::get(std::size_t row_ndx) { Row ObjectArray::get(std::size_t row_ndx) {
verify_attached();
if (row_ndx >= link_view->size()) { if (row_ndx >= link_view->size()) {
throw std::range_error(std::string("Index ") + std::to_string(row_ndx) + " is outside of range 0..." + throw std::range_error(std::string("Index ") + std::to_string(row_ndx) + " is outside of range 0..." +
std::to_string(link_view->size()) + "."); std::to_string(link_view->size()) + ".");
@ -44,10 +42,17 @@ void ObjectArray::verify_attached() {
link_view->sync_if_needed(); link_view->sync_if_needed();
} }
static inline ObjectArray * RJSVerifiedArray(JSObjectRef object) {
ObjectArray *array = RJSGetInternal<ObjectArray *>(object);
array->verify_attached();
return array;
}
JSValueRef ArrayGetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* jsException) { JSValueRef ArrayGetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* jsException) {
try { try {
// index subscripting // index subscripting
ObjectArray *array = RJSGetInternal<ObjectArray *>(object); ObjectArray *array = RJSVerifiedArray(object);
array->verify_attached();
size_t size = array->size(); size_t size = array->size();
std::string indexStr = RJSStringForJSString(propertyName); std::string indexStr = RJSStringForJSString(propertyName);
@ -71,6 +76,7 @@ JSValueRef ArrayGetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef pr
void ArrayPropertyNames(JSContextRef ctx, JSObjectRef object, JSPropertyNameAccumulatorRef propertyNames) { void ArrayPropertyNames(JSContextRef ctx, JSObjectRef object, JSPropertyNameAccumulatorRef propertyNames) {
ObjectArray *array = RJSGetInternal<ObjectArray *>(object); ObjectArray *array = RJSGetInternal<ObjectArray *>(object);
char str[32]; char str[32];
for (int i = 0; i < array->link_view->size(); i++) { for (int i = 0; i < array->link_view->size(); i++) {
sprintf(str, "%i", i); sprintf(str, "%i", i);
@ -82,7 +88,7 @@ void ArrayPropertyNames(JSContextRef ctx, JSObjectRef object, JSPropertyNameAccu
JSValueRef ArrayPush(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException) { JSValueRef ArrayPush(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException) {
try { try {
ObjectArray *array = RJSGetInternal<ObjectArray *>(thisObject); ObjectArray *array = RJSVerifiedArray(thisObject);
RJSValidateArgumentCountIsAtLeast(argumentCount, 1); RJSValidateArgumentCountIsAtLeast(argumentCount, 1);
for (size_t i = 0; i < argumentCount; i++) { for (size_t i = 0; i < argumentCount; i++) {
array->link_view->add(RJSAccessor::to_object_index(ctx, array->realm, const_cast<JSValueRef &>(arguments[i]), array->object_schema.name, false)); array->link_view->add(RJSAccessor::to_object_index(ctx, array->realm, const_cast<JSValueRef &>(arguments[i]), array->object_schema.name, false));
@ -99,7 +105,7 @@ JSValueRef ArrayPush(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObj
JSValueRef ArrayPop(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException) { JSValueRef ArrayPop(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException) {
try { try {
ObjectArray *array = RJSGetInternal<ObjectArray *>(thisObject); ObjectArray *array = RJSVerifiedArray(thisObject);
RJSValidateArgumentCount(argumentCount, 0); RJSValidateArgumentCount(argumentCount, 0);
if (array->link_view->size() == 0) { if (array->link_view->size() == 0) {
return JSValueMakeUndefined(ctx); return JSValueMakeUndefined(ctx);
@ -119,7 +125,7 @@ JSValueRef ArrayPop(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObje
JSValueRef ArrayUnshift(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException) { JSValueRef ArrayUnshift(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException) {
try { try {
ObjectArray *array = RJSGetInternal<ObjectArray *>(thisObject); ObjectArray *array = RJSVerifiedArray(thisObject);
RJSValidateArgumentCountIsAtLeast(argumentCount, 1); RJSValidateArgumentCountIsAtLeast(argumentCount, 1);
for (size_t i = 0; i < argumentCount; i++) { for (size_t i = 0; i < argumentCount; i++) {
array->link_view->insert(i, RJSAccessor::to_object_index(ctx, array->realm, const_cast<JSValueRef &>(arguments[i]), array->object_schema.name, false)); array->link_view->insert(i, RJSAccessor::to_object_index(ctx, array->realm, const_cast<JSValueRef &>(arguments[i]), array->object_schema.name, false));
@ -136,9 +142,9 @@ JSValueRef ArrayUnshift(JSContextRef ctx, JSObjectRef function, JSObjectRef this
JSValueRef ArrayShift(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException) { JSValueRef ArrayShift(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException) {
try { try {
ObjectArray *array = RJSGetInternal<ObjectArray *>(thisObject); ObjectArray *array = RJSVerifiedArray(thisObject);
RJSValidateArgumentCount(argumentCount, 0); RJSValidateArgumentCount(argumentCount, 0);
if (array->link_view->size() == 0) { if (array->size() == 0) {
return JSValueMakeUndefined(ctx); return JSValueMakeUndefined(ctx);
} }
JSValueRef obj = RJSObjectCreate(ctx, Object(array->realm, array->object_schema, array->get(0))); JSValueRef obj = RJSObjectCreate(ctx, Object(array->realm, array->object_schema, array->get(0)));
@ -155,15 +161,15 @@ JSValueRef ArrayShift(JSContextRef ctx, JSObjectRef function, JSObjectRef thisOb
JSValueRef ArraySplice(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException) { JSValueRef ArraySplice(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException) {
try { try {
ObjectArray *array = RJSGetInternal<ObjectArray *>(thisObject); ObjectArray *array = RJSVerifiedArray(thisObject);
RJSValidateArgumentCountIsAtLeast(argumentCount, 2); RJSValidateArgumentCountIsAtLeast(argumentCount, 2);
long index = RJSValidatedValueToNumber(ctx, arguments[0]); long index = RJSValidatedValueToNumber(ctx, arguments[0]);
if (index < 0) { if (index < 0) {
index = array->link_view->size() - index; index = array->size() + index;
} }
long remove = RJSValidatedValueToNumber(ctx, arguments[1]); long remove = RJSValidatedValueToNumber(ctx, arguments[1]);
if (index + remove >= array->link_view->size()) { if (index + remove > array->size()) {
throw std::runtime_error("Attempting to slice elements beyond Array bounds."); throw std::runtime_error("Attempting to slice elements beyond Array bounds.");
} }

View File

@ -137,9 +137,10 @@ var ArrayTests = {
var realm = new Realm({schema: [LinkTypesObjectSchema, TestObjectSchema]}); var realm = new Realm({schema: [LinkTypesObjectSchema, TestObjectSchema]});
var array; var array;
realm.write(function() { realm.write(function() {
var obj = realm.create('LinkTypesObject', [[1], [2], [[3]]]); var obj = realm.create('LinkTypesObject', [[1], [2], [[3], [4]]]);
array = obj.arrayCol; array = obj.arrayCol;
TestCase.assertEqual(array.pop().doubleCol, 4);
TestCase.assertEqual(array.pop().doubleCol, 3); TestCase.assertEqual(array.pop().doubleCol, 3);
TestCase.assertEqual(array.length, 0); TestCase.assertEqual(array.length, 0);
@ -179,4 +180,65 @@ var ArrayTests = {
// }); // });
}, },
testShift: function() {
var realm = new Realm({schema: [LinkTypesObjectSchema, TestObjectSchema]});
var array;
realm.write(function() {
var obj = realm.create('LinkTypesObject', [[1], [2], [[3], [4]]]);
array = obj.arrayCol;
TestCase.assertEqual(array.shift().doubleCol, 3);
TestCase.assertEqual(array.shift().doubleCol, 4);
TestCase.assertEqual(array.length, 0);
TestCase.assertEqual(array.shift(), undefined);
TestCase.assertThrows(function() {
array.shift(1);
});
});
// TestCase.assertThrows(function() {
// array.shift();
// });
},
testSplice: function() {
var realm = new Realm({schema: [LinkTypesObjectSchema, TestObjectSchema]});
var array;
realm.write(function() {
var obj = realm.create('LinkTypesObject', [[1], [2], [[3], [4]]]);
array = obj.arrayCol;
TestCase.assertEqual(array.splice(0, 0, obj.objectCol, obj.objectCol1), 4);
TestCase.assertEqual(array.length, 4);
TestCase.assertEqual(array[0].doubleCol, 1);
TestCase.assertEqual(array[1].doubleCol, 2);
TestCase.assertEqual(array.splice(2, 2, [5], [6]), 4);
TestCase.assertEqual(array.length, 4);
TestCase.assertEqual(array[2].doubleCol, 5);
TestCase.assertEqual(array[3].doubleCol, 6);
TestCase.assertEqual(array.splice(2, 2), 2);
TestCase.assertEqual(array[0].doubleCol, 1);
TestCase.assertEqual(array[1].doubleCol, 2);
TestCase.assertEqual(array.length, 2);
TestCase.assertEqual(array.splice(-1, 1), 1);
TestCase.assertEqual(array.length, 1);
TestCase.assertEqual(array[0].doubleCol, 1);
TestCase.assertThrows(function() {
array.splice(0, 2);
});
TestCase.assertThrows(function() {
array.splice(0, 0, 0);
});
});
// TestCase.assertThrows(function() {
// array.shift();
// });
},
}; };