📄 sof_02.cc
字号:
count++; if (sof0.read(&j, sizeof(j), 1) != 1) { return Error::handle(name(), L"read", Error::TEST, __FILE__, __LINE__); } if (j != val) { return Error::handle(name(), L"mismatch", Error::TEST, __FILE__, __LINE__); } } } // check count // if (count != NUM_CLASSES * NUM_TAGS) { return Error::handle(name(), L"not enough", Error::TEST, __FILE__, __LINE__); } // test delete // if (level_a > Integral::BRIEF) { Console::put(L"testing remove (binary)"); } // go back through, delete every other entry // for (long i = 0; i < NUM_CLASSES; i += 2) { // create a name // SysString cname; cname.assign((long)i); cname.insert(L"LongerNamedEntity", 0); if ((i % 4) == 0) { // read all the nodes of this name // for (tag = sof0.first(cname); tag != Sof::NO_TAG; tag = sof0.next(cname, tag)) { // remove node // if (!sof0.remove(cname, tag)) { return Error::handle(name(), L"remove1", Error::TEST, __FILE__, __LINE__); } } } // delete all instances of this name // else if (!sof0.remove(cname)) { return Error::handle(name(),L"remove2",Error::TEST,__FILE__,__LINE__); } if (sof0.getCount(cname) != 0) { return Error::handle(name(), L"remove", Error::TEST, __FILE__, __LINE__); } } sof0.cleanUp(); if (sof0.getCount() != 0) { return Error::handle(name(), L"remove", Error::TEST, __FILE__, __LINE__); } sof0.close(); // testing read after delete // if (level_a > Integral::BRIEF) { SysString output(L"testing read after delete (binary)"); Console::put(output); } // open the file again // sof0.open(file1); count = 0; // read all the dynamically named nodes // for (long i = 1; i < NUM_CLASSES; i += 2) { // create a name // SysString cname; cname.assign((long)i); cname.insert(L"LongerNamedEntity", 0); // read all the nodes of this name // for (tag = sof0.first(cname); tag != Sof::NO_TAG; tag = sof0.next(cname, tag)) { // find the node // if (!sof0.find(cname, tag)) { return Error::handle(name(), L"find", Error::TEST, __FILE__, __LINE__); } long val = i * 10277 + tag * 13; long j; count++; if (sof0.read(&j, sizeof(j), 1) != 1) { return Error::handle(name(), L"read", Error::TEST, __FILE__, __LINE__); } if (j != val) { return Error::handle(name(),L"mismatch",Error::TEST,__FILE__,__LINE__); } } } if (count != NUM_CLASSES * NUM_TAGS / 2) { return Error::handle(name(),L"not enough 2",Error::TEST,__FILE__,__LINE__); } // close the file // sof0.close(); // add a whole bunch of names to the text file // NUM_CLASSES = 20; sof2.open(file2, File::WRITE_PLUS); for (long i = 0; i < NUM_CLASSES; i++) { // create a name // SysString cname; cname.assign((long)i); cname.insert(L"LongerNamedEntity", 0); // add multiple nodes // for (long j = 0; j < NUM_TAGS; j++) { // add the node // sof2.put(cname, j, ANY_SIZE); // create and write a value // long val = i * 10277 + j * 13; // write the text value // output.assign((long)val); output.insert(L"value = ", 0); output.concat(L"\n"); sof2.puts(output); } } // close the files // sof2.close(); // reset indentation // if (level_a > Integral::NONE) { Console::decreaseIndention(); } //-------------------------------------------------------------------------- // // 5. class-specific public methods: // input and output methods // //-------------------------------------------------------------------------- // set indentation // if (level_a > Integral::NONE) { Console::put(L"testing class-specific public methods: i/o methods...\n"); Console::increaseIndention(); } // open the text file again // sof0.open(file2, File::READ_PLUS); count = 0; SysString input; // read all the dynamically named nodes // for (long i = 0; i < NUM_CLASSES; i++) { // create a name // SysString cname; cname.assign((long)i); cname.insert(L"LongerNamedEntity", 0); // read all the nodes of this name // for (tag = sof0.first(cname); tag != Sof::NO_TAG; tag = sof0.next(cname, tag)) { // find the node // if (!sof0.find(cname, tag)) { return Error::handle(name(), L"find", Error::TEST, __FILE__, __LINE__); } long val = i * 10277 + tag * 13; long j; count++; if ((!sof0.gets(input)) || (input.firstStr(L"value = ") != 0)) { return Error::handle(name(), L"gets", Error::TEST, __FILE__, __LINE__); } SysString numeric; input.substr(numeric, 8); if (!numeric.get(j)) { return Error::handle(name(), L"gets", Error::TEST, __FILE__, __LINE__); } if (j != val) { return Error::handle(name(),L"mismatch",Error::TEST,__FILE__,__LINE__); } } } // check count // if (count != NUM_CLASSES * NUM_TAGS) { return Error::handle(name(), L"not enough",Error::TEST,__FILE__,__LINE__); } // test delete // if (level_a > Integral::BRIEF) { SysString output(L"testing remove (text)"); Console::put(output); } // go back through, delete every other entry // for (long i = 0; i < NUM_CLASSES; i += 2) { // create a name // SysString cname; cname.assign((long)i); cname.insert(L"LongerNamedEntity", 0); if ((i % 4) == 0) { // read all the nodes of this name // for (tag = sof0.first(cname); tag != Sof::NO_TAG; tag = sof0.next(cname, tag)) { // remove node // if (!sof0.remove(cname, tag)) { return Error::handle(name(), L"remove1", Error::TEST, __FILE__, __LINE__); } } } // delete all instances of this name // else if (!sof0.remove(cname)) { return Error::handle(name(), L"remove2", Error::TEST, __FILE__, __LINE__); } } sof0.close(); // testing read after delete // if (level_a > Integral::BRIEF) { Console::put(L"testing read after delete (text)"); } // open the file again // sof0.open(file2); count = 0; // read all the dynamically named nodes // for (long i = 1; i < NUM_CLASSES; i += 2) { // create a name // SysString cname; cname.assign((long)i); cname.insert(L"LongerNamedEntity", 0); // read all the nodes of this name // for (tag = sof0.first(cname); tag != Sof::NO_TAG; tag = sof0.next(cname, tag)) { // find the node // if (!sof0.find(cname, tag)) { return Error::handle(name(), L"find", Error::TEST, __FILE__, __LINE__); } long val = i * 10277 + tag * 13; long j; count++; if ((!sof0.gets(input)) || (input.firstStr(L"value = ") != 0)) { return Error::handle(name(), L"gets", Error::TEST, __FILE__, __LINE__); } SysString numeric; input.substr(numeric, 8); if (!numeric.get(j)) { return Error::handle(name(), L"gets", Error::TEST, __FILE__, __LINE__); } if (j != val) { return Error::handle(name(), L"mismatch", Error::TEST, __FILE__, __LINE__); } } } if (count != NUM_CLASSES * NUM_TAGS / 2) { return Error::handle(name(), L"not enough 2", Error::TEST, __FILE__, __LINE__); } // test enumerate method // SofList sof0_index; SofSymbolTable sof0_table; if (!sof0.enumerate(sof0_index, sof0_table)) { return Error::handle(name(), L"enumerate", Error::TEST, __FILE__, __LINE__); } // close the file // sof0.close(); // test overwriting in binary w/resize // { SysString temp; Integral::makeTemp(temp); Sof sof; sof.open(temp, File::WRITE_ONLY, File::BINARY); SysString obj_name(L"MyObject"); sof.put(obj_name, 0, sizeof(long)); long a = 5; sof.write(&a, sizeof(long), 1); sof.close(); sof.open(temp, File::READ_PLUS, File::BINARY); long pos = sof.end_of_data_d; sof.put(obj_name, 0, sizeof(long)*2); sof.write(&a, sizeof(long), 1); sof.write(&a, sizeof(long), 1); if (sof.end_of_data_d != (pos + (long)sizeof(long))) { return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__); } sof.close(); } // reset indentation // if (level_a > Integral::NONE) { Console::decreaseIndention(); } //-------------------------------------------------------------------------- // // 6. class-specific public methods: // formatting methods // //-------------------------------------------------------------------------- // set indentation // if (level_a > Integral::NONE) { Console::put(L"testing class-specific public methods: formatting methods...\n"); Console::increaseIndention(); } // open the file again // sof0.open(file0, File::WRITE_ONLY); // increase indentation // sof0.increaseIndention(); sof0.setLineWrap(100); if (sof0.getLineWrap() != 100) { return Error::handle(name(), L"set/getLineWrap", Error::TEST, __FILE__, __LINE__); } if (sof0.getLineLength() != 100 - (File::INDENT_STR).length()) { return Error::handle(name(), L"set/getLineLength", Error::TEST, __FILE__, __LINE__); } // decrease indentation // sof0.decreaseIndention(); if (sof0.getLineLength() != 100) { return Error::handle(name(), L"set/getLineLength", Error::TEST, __FILE__, __LINE__); } sof0.close(); // delete temporary files // //File::remove(file0); //File::remove(file1); //File::remove(L"/tmp/foo_asc.sof"); //File::remove(L"/tmp/foo_copy.sof"); // reset indentation // if (level_a > Integral::NONE) { Console::decreaseIndention(); } //--------------------------------------------------------------------------- // // 7. print completion message // //--------------------------------------------------------------------------- // reset indentation // if (level_a > Integral::NONE) { Console::decreaseIndention(); } if (level_a > Integral::NONE) { SysString output(L"diagnostics passed for class "); output.concat(name()); output.concat(L"\n"); Console::put(output); } // exit gracefully // return true;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -