📄 teststruct.cpp
字号:
/******************************************************************* * * * teststruct.cpp * * * * This file is a part of the eXtremeDB source code * * Copyright (c) 2001-2007 McObject LLC * * All Rights Reserved * * * *******************************************************************/#include "teststruct.h"void storeData(SqlEngine& engine, int i) { MyRecord r; char buf[256]; sprintf(buf, "Record %d", i); r.s = buf; r.i2 = i*10; r.p.x = i+1; r.p.y = i+2; r.i1 = i%10; r.i8 = i*100; r.f = (float)i/10; r.d = (double)i/100; r.l.beg.x = i+2; r.l.beg.y = i+2; r.l.end.x = i+3; r.l.end.y = i+4; r.i4 = i; r.ai1 = NULL; r.vp = NULL; r.lines = NULL; engine.executeStatement("insert into MyRecord %r", &r);} void fetchData(SqlEngine& engine, int i, bool fetchArrays) { char buf[256]; MyRecord r; QueryResult result(engine.executeQuery("select * from MyRecord where i4=%i", i)); Cursor* cursor = result->records(); Record* rec = cursor->next(); result->extract(rec, &r, sizeof(r)); sprintf(buf, "Record %d", i); assert(strcmp(r.s, buf) == 0); assert(r.i2 == i*10); assert(r.p.x == i+1); assert(r.p.y == i+2); assert(r.i1 == i%10); assert(r.i8 == i*100); assert((int)((r.f+EPSILON)*10) == i); assert((int)((r.d+EPSILON)*100) == i); assert(r.l.beg.x == i+2); assert(r.l.beg.y == i+2); assert(r.l.end.x == i+3); assert(r.l.end.y == i+4); assert(r.i4 == i); if (fetchArrays) { int j, k; assert(r.ai1->size() == 100); r.ai1->getBody(buf, 0, 100); for (j = 0; j < 100; j++) { assert(buf[j] == i % 100); } assert(r.vp->size() == i % 10); for (j = 0; j < i % 10; j++) { Struct* poly = (Struct*)r.vp->getAt(j); Array* points = (Array*)poly->get(0); assert(points->size() == j); for (k = 0; k < j; k++) { Struct* point = (Struct*)points->getAt(k); assert(point->get(0)->intValue() == j*k); assert(point->get(1)->intValue() == j*k+1); } } assert(r.lines->size() == 10); for (j = 0; j < 10; j++) { Struct* line = (Struct*)r.lines->getAt(j); Struct* beg = (Struct*)line->get(0); Struct* end = (Struct*)line->get(1); assert(beg->get(0)->intValue() == j-1); assert(beg->get(1)->intValue() == j-2); assert(end->get(0)->intValue() == j+1); assert(end->get(1)->intValue() == j+2); } }}void updateData(SqlEngine& engine, int i){ int j, k; char buf[100]; MyRecord r; QueryResult result(engine.executeQuery("select * from MyRecord where i4=%i for update", i)); Cursor* cursor = result->records(); Record* rec = cursor->next(); Field* vp = result->findField("vp"); Field* points = vp->element()->findComponent("points"); Field* x = points->element()->findComponent("x"); Field* y = points->element()->findComponent("y"); Field* lines = result->findField("lines"); Field* beg = lines->element()->findComponent("beg"); Field* end = lines->element()->findComponent("end"); result->extract(rec, &r, sizeof(r)); memset(buf, i % 100, 100); r.ai1->setBody(buf, 0, 100); r.vp->setSize(i % 10); for (j = 0; j < i % 10; j++) { Struct* poly = (Struct*)r.vp->updateAt(j); Array* pa = (Array*)points->update(poly); pa->setSize(j); for (k = 0; k < j; k++) { Struct* point = (Struct*)pa->updateAt(k); x->set(point, new IntValue(j*k)); y->set(point, new IntValue(j*k+1)); } } for (j = 0; j < 10; j++) { Struct* line = (Struct*)r.lines->updateAt(j); Struct* bp = (Struct*)beg->update(line); Struct* ep = (Struct*)end->update(line); x->set(bp, new IntValue(j-1)); y->set(bp, new IntValue(j-2)); x->set(ep, new IntValue(j+1)); y->set(ep, new IntValue(j+2)); } rec->updateRecord();}void scanArray(SqlEngine& engine) { int i; MyRecord r; QueryResult result(engine.executeQuery("select * from MyRecord where %i in ai1", 99)); Cursor* cursor = result->records(); for (i = 0; cursor->hasNext(); i++) { Record* rec = cursor->next(); result->extract(rec, &r, sizeof(r)); assert(r.i4 % 100 == 99); } assert(i == (nRecords+1)/100);}void searchArray(SqlEngine& engine) { int i; MyRecord r; QueryResult result(engine.executeQuery("select * from MyRecord where exists i:(exists j:(vp[i].points[j].x=%i))", 7*8)); Cursor* cursor = result->records(); for (i = 0; cursor->hasNext(); i++) { Record* rec = cursor->next(); result->extract(rec, &r, sizeof(r)); assert(r.i4 % 10 == 9); } assert(i == (nRecords+1)/10);}int main(){ int i; McoSqlEngine engine; engine.open("teststructdb", teststructdb_get_dictionary(), DATABASE_SIZE, PAGE_SIZE, MAP_ADDRESS); for (i = 0; i < nRecords; i++) { storeData(engine, i); } for (i = 0; i < nRecords; i++) { fetchData(engine, i, false); } for (i = 0; i < nRecords; i++) { updateData(engine, i); } for (i = 0; i < nRecords; i++) { fetchData(engine, i, true); } searchArray(engine); scanArray(engine); engine.close(); printf("Test passed\n"); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -