z3 / src / test / optional.cpp

/*++
Copyright (c) 2006 Microsoft Corporation

Module Name:

    tst_optional.cpp

Abstract:

    Test optional module

Author:

    Leonardo de Moura (leonardo) 2006-09-29.

Revision History:

--*/
#include"trace.h"
#include"debug.h"
#include"optional.h"

static void tst1() {
    optional<int> v;
    SASSERT(!v);
    SASSERT(v == false);
    v = 10;
    SASSERT(v);
    SASSERT(*v == 10);
    TRACE("optional", tout << sizeof(v) << "\n";);
}

struct OptFoo {
    int m_x;
    int m_y;

    OptFoo(int x, int y):m_x(x), m_y(y) {
	TRACE("optional", tout << "OptFoo created: " << m_x << " : " << m_y << "\n";);
    }

    ~OptFoo() {
	TRACE("optional", tout << "OptFoo deleted: " << m_x << " : " << m_y << "\n";);
    }
};

static void tst2() {
    optional<OptFoo> v;
    SASSERT(!v);
    v = OptFoo(10, 20);
    SASSERT(v->m_x == 10);
    SASSERT(v->m_y == 20);
    v = OptFoo(200, 300);
    SASSERT(v->m_x == 200);
    SASSERT(v->m_y == 300);
    TRACE("optional", tout << sizeof(v) << "\n";);
}

static void tst3() {
    optional<int *> v;
    SASSERT(!v);
    int x = 10;
    v = &x;
    SASSERT(v);
    SASSERT(*v == &x);
    TRACE("optional", tout << sizeof(v) << "\n";);
    SASSERT(*(*v) == 10);
}

void tst_optional() {
    tst1();
    tst2();
    tst3();
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.