Example #1
0
void DicomDict::deserialize(XmlDeserializer &d) throw (tgt::Exception) {
    //get Vector of DictEntries
    std::vector<DicomDictEntry> entryVector;

    //deserialize Vector
    d.setUseAttributes(false);
    d.deserialize("Dict", entryVector, "entry");

    //iterate over Vector and put in map
    std::vector<DicomDictEntry>::iterator it;
    for (it = entryVector.begin(); it != entryVector.end(); ++it) {
        //check if keyword is already in entries_
        if (entries_.find(it->getKeyword()) != entries_.end()) {
            throw tgt::Exception("Keyword in Dictionary must be unique! Found keyword more than once: " + it->getKeyword());
        }

        entries_[it->getKeyword()] = *it;
    }
}
Example #2
0
/**
 * Tests serialization and deserialization of @c AbstractSerializable null pointers.
 */
void testBugAbstractSerializableNullPointerSerialization() {
    Abstract* a = 0;

    std::stringstream stream;

    AbstractFactory factory;

    XmlSerializer s;
    s.setUseAttributes(true);
    s.registerFactory(&factory);
    s.serialize("Abstract", a);
    s.write(stream);

    XmlDeserializer d;
    d.setUseAttributes(true);
    d.registerFactory(&factory);
    d.read(stream);
    try {
        d.deserialize("Abstract", a);
    }
    catch (XmlSerializationMemoryAllocationException&) {
        test(false, "bug occured, since memory allocation exception is thrown for 0 pointer");
    }
}
Example #3
0
/**
 * Tests serialization and deserialization of STL sequence containers with 'useAttributes' enabled
 */
void testSequenceContainersUseAttributes() {
    std::vector<int> v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);

    std::vector<uint8_t> vec_uint8;
    vec_uint8.push_back(1);
    vec_uint8.push_back(2);
    vec_uint8.push_back(3);

    std::vector<int8_t> vec_int8;
    vec_int8.push_back(1);
    vec_int8.push_back(-2);
    vec_int8.push_back(3);

    std::vector<uint16_t> vec_uint16;
    vec_uint16.push_back(1);
    vec_uint16.push_back(2);
    vec_uint16.push_back(3);

    std::vector<uint64_t> vec_uint64;
    vec_uint64.push_back(1);
    vec_uint64.push_back(2);
    vec_uint64.push_back(1<<30);

    std::vector<int> numbers;
    numbers.push_back(4);
    numbers.push_back(5);

    std::deque<int> deque;
    deque.push_back(1);
    deque.push_back(2);
    deque.push_back(3);

    std::list<int> list;
    list.push_back(1);
    list.push_back(2);
    list.push_back(3);

    std::stringstream stream;

    XmlSerializer s;
    s.setUseAttributes(true);
    s.serialize("v", v);
    s.serialize("vec_uint8", vec_uint8);
    s.serialize("vec_int8", vec_int8);
    s.serialize("vec_uint16", vec_uint16);
    s.serialize("vec_uint64", vec_uint64);
    s.serialize("numbers", numbers, "number");
    s.serialize("deque", deque);
    s.serialize("list", list);
    s.write(stream);

    // Reset all variables to default values...
    v.clear();
    test(v.size() == 0, "vector v is not empty");
    numbers.clear();
    test(numbers.size() == 0, "vector numbers is not empty");
    deque.clear();
    test(deque.size() == 0, "deque is not empty");
    list.clear();
    test(list.size() == 0, "list is not empty");

    XmlDeserializer d;
    d.setUseAttributes(true);
    d.read(stream);
    d.deserialize("v", v);
    d.deserialize("vec_uint8", vec_uint8);
    d.deserialize("vec_int8", vec_int8);
    d.deserialize("vec_uint16", vec_uint16);
    d.deserialize("vec_uint64", vec_uint64);
    d.deserialize("numbers", numbers, "number");
    d.deserialize("deque", deque);
    d.deserialize("list", list);

    test(v.size() == 3, "v: not all vector items deserialized");
    test(v[0], 1, "v: first item incorrect deserialized");
    test(v[1], 2, "v: second item incorrect deserialized");
    test(v[2], 3, "v: third item incorrect deserialized");

    test(vec_uint8.size() == 3, "vec_uint8: not all vector items deserialized");
    test(vec_uint8[0], (uint8_t)1, "vec_uint8: first item incorrect deserialized");
    test(vec_uint8[1], (uint8_t)2, "vec_uint8: second item incorrect deserialized");
    test(vec_uint8[2], (uint8_t)3, "vec_uint8: third item incorrect deserialized");

    test(vec_int8.size() == 3, "vec_int8: not all vector items deserialized");
    test(vec_int8[0], (int8_t)1, "vec_int8: first item incorrect deserialized");
    test(vec_int8[1], (int8_t)-2, "vec_int8: second item incorrect deserialized");
    test(vec_int8[2], (int8_t)3, "vec_int8: third item incorrect deserialized");

    test(vec_uint16.size() == 3, "vec_uint16: not all vector items deserialized");
    test(vec_uint16[0], (uint16_t)1, "vec_uint16: first item incorrect deserialized");
    test(vec_uint16[1], (uint16_t)2, "vec_uint16: second item incorrect deserialized");
    test(vec_uint16[2], (uint16_t)3, "vec_uint16: third item incorrect deserialized");

    test(vec_uint64.size() == 3, "vec_uint64: not all vector items deserialized");
    test(vec_uint64[0], (uint64_t)1, "vec_uint64: first item incorrect deserialized");
    test(vec_uint64[1], (uint64_t)2, "vec_uint64: second item incorrect deserialized");
    test(vec_uint64[2], (uint64_t)(1<<30), "vec_uint64: third item incorrect deserialized");

    test(numbers.size() == 2, "numbers: not all vector items deserialized");
    test(numbers[0], 4, "numbers: first item incorrect deserialized");
    test(numbers[1], 5, "numbers: second item incorrect deserialized");

    test(deque.size() == 3, "deque: not all deque items deserialized");
    test(deque[0], 1, "deque: first item incorrect deserialized");
    test(deque[1], 2, "deque: second item incorrect deserialized");
    test(deque[2], 3, "deque: third item incorrect deserialized");

    test(list.size() == 3, "list: not all list items deserialized");
    test(list.front(), 1, "list: first item incorrect deserialized");
    list.pop_front();
    test(list.front(), 2, "list: second item incorrect deserialized");
    list.pop_front();
    test(list.front(), 3, "list: third item incorrect deserialized");
}
Example #4
0
/**
 * Tests serialization and deserialization using pointer content serialization
 * mixed up with using XML attributes.
 *
 * @attention This is a grey box test which uses implementation details
 *            to keep the test as short as possible. That is why
 *            you have to consider changing the test every time
 *            the implementation details have changed.
 */
void testUsePointerContentSerialization() {
    int i = 1;
    int* ip = &i;
    std::vector<int*> v;
    v.push_back(new int(2));
    std::map<std::string, int*> m;
    m["three"] = new int(3);

    std::stringstream stream;

    XmlSerializer s;
    s.setUsePointerContentSerialization(true);
    s.serialize("i", i);
    s.serialize("ip", ip);
    s.serialize("v", v);
    s.serialize("m", m, "number");
    s.setUseAttributes(true);
    s.serialize("ai", i);
    s.serialize("aip", ip);
    s.serialize("av", v);
    s.serialize("am", m, "number");
    s.write(stream);

    // Reset all variables to default values...
    i = 0;
    ip = new int(0);
    *v[0] = 0;
    *m["three"] = 0;

    int ai = 0;
    int* aip = new int(0);
    std::vector<int*> av;
    av.push_back(new int(0));
    std::map<std::string, int*> am;
    am["three"] = new int(0);

    XmlDeserializer d;
    d.read(stream);
    d.setUsePointerContentSerialization(true);
    d.deserialize("i", i);
    d.deserialize("ip", ip);
    d.deserialize("v", v);
    d.deserialize("m", m, "number");
    d.setUseAttributes(true);
    d.deserialize("ai", ai);
    d.deserialize("aip", aip);
    d.deserialize("av", av);
    d.deserialize("am", am, "number");

    test(i, 1, "i incorrect deserialized");
    test(ip != &i, "ip is pointing to adress of i");
    test(*ip, 1, "ip incorrect deserialized");
    test(v.size() == 1, "v: incorrect size");
    test(*v[0], 2, "v: first item incorrect deserialized");
    test(m.size() == 1, "m: incorrect size");
    test(*m["three"], 3, "m: first item incorrect deserialized");

    test(ai, 1, "ai incorrect deserialized");
    test(aip != &ai, "aip is pointing to adress of ai");
    test(*aip, 1, "aip incorrect deserialized");
    test(av.size() == 1, "av: incorrect size");
    test(*av[0], 2, "av: first item incorrect deserialized");
    test(am.size() == 1, "am: incorrect size");
    test(*am["three"], 3, "am: first item incorrect deserialized");

    delete ip;
    delete v[0];
    delete m["three"];
    delete aip;
    delete av[0];
    delete am["three"];
}
Example #5
0
/**
 * Tests serialization and deserialization using XML attributes.
 *
 * @attention This is a grey box test which uses implementation details
 *            to keep the test as short as possible. That is why
 *            you have to consider changing the test every time
 *            the implementation details have changed.
 */
void testUseAttributes() {
    int i = 1;
    std::string str = "short string";
    std::string strn = "string\nwith\nnew\nlines";
    tgt::ivec2 vec(2, 3);

    std::vector<int> v;
    v.push_back(4);
    v.push_back(5);

    std::map<int, std::string> m;
    m[6] = "six";
    m[7] = "seven";

    std::vector<tgt::ivec2> vv;
    vv.push_back(tgt::ivec2(8, 9));

    std::stringstream stream;

    XmlSerializer s;
    s.setUseAttributes(true);
    s.serialize("i", i);
    s.serialize("str", str);
    s.serialize("strn", strn);
    s.serialize("vec", vec);
    s.serialize("v", v);
    s.serialize("m", m);
    s.serialize("vv", vv);
    s.write(stream);

    // Reset all variables to default values...
    i = 0;
    std::string dstr = "";
    std::string dstrn = "";
    vec = tgt::ivec2(0, 0);
    v.clear();
    m.clear();
    vv.clear();

    test(v.size() == 0, "v not empty");
    test(m.size() == 0, "m not empty");
    test(vv.size() == 0, "vv not empty");

    XmlDeserializer d;
    d.setUseAttributes(true);
    d.read(stream);
    d.deserialize("i", i);
    d.deserialize("str", dstr);
    d.deserialize("strn", dstrn);
    d.deserialize("vec", vec);
    d.deserialize("v", v);
    d.deserialize("m", m);
    d.deserialize("vv", vv);

    test(i, 1, "i incorrect deserialized");
    test(str == dstr, "str incorrect deserialized");
    test(strn == dstrn, "strn incorrect deserialized");
    test(vec.x, 2, "vec.x incorrect deserialized");
    test(vec.y, 3, "vec.y incorrect deserialized");
    test(v.size() == 2, "v: not all items deserialized");
    test(v[0], 4, "v: first item incorrect deserialized");
    test(v[1], 5, "v: second item incorrect deserialized");
    test(m.size() == 2, "m: not all items deserialized");
    test(m[6] == "six", "m: first item incorrect deserialized");
    test(m[7] == "seven", "m: second item incorrect deserialized");
    test(vv.size() == 1, "vv: not all items deserialized");
    test(vv[0].x, 8, "vv: first item x component incorrect deserialized");
    test(vv[0].y, 9, "vv: first item y component incorrect deserialized");
}