1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/* *****************************************************************
    MESQUITE -- The Mesh Quality Improvement Toolkit

    Copyright 2006 Lawrence Livermore National Laboratory.  Under
    the terms of Contract B545069 with the University of Wisconsin --
    Madison, Lawrence Livermore National Laboratory retains certain
    rights in this software.

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    (lgpl.txt) along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    (2006) [email protected]

  ***************************************************************** */

/** \file TagVertexMeshTest.cpp
 *  \brief unit tests for TagVertexMesh class
 *  \author Jason Kraftcheck
 */

#include "Mesquite.hpp"
#include "TagVertexMesh.hpp"
#include "MsqError.hpp"
#include "UnitUtil.hpp"
#include "MeshImpl.hpp"
#include "MsqVertex.hpp"
#include "InstructionQueue.hpp"
#include <cppunit/extensions/HelperMacros.h>
#include <cstdio>

using namespace MBMesquite;

const char TEMP_FILE_NAME[] = "TagVertexMesh.vtk";

class TagVertexMeshTest : public CppUnit::TestFixture
{
  private:
    CPPUNIT_TEST_SUITE( TagVertexMeshTest );
    CPPUNIT_TEST( test_vertex_coordinates );
    CPPUNIT_TEST( test_save_coordinates );
    CPPUNIT_TEST( test_cleanup );
    CPPUNIT_TEST( test_alternate_name );
    CPPUNIT_TEST( test_reference_mesh );
    CPPUNIT_TEST_SUITE_END();

    MeshImpl* realMesh;

  public:
    TagVertexMeshTest() : realMesh( 0 ) {}

    void setUp();
    void tearDown();

    void test_vertex_coordinates();
    void test_save_coordinates();
    void test_cleanup();
    void test_alternate_name();
    void test_reference_mesh();
};

CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TagVertexMeshTest, "TagVertexMeshTest" );
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TagVertexMeshTest, "Unit" );

void TagVertexMeshTest::setUp()
{
    const char vtk_data[] = "# vtk DataFile Version 2.0\n"
                            "test mesh\n"
                            "ASCII\n"
                            "DATASET UNSTRUCTURED_GRID\n"
                            "POINTS 3 float\n"
                            "0 0 0\n"
                            "1 0 0\n"
                            "0 1 0\n"
                            "CELLS 1 4\n"
                            "3 0 1 2\n"
                            "CELL_TYPES 1\n"
                            "5\n";

    FILE* file = fopen( TEMP_FILE_NAME, "w" );<--- Assignment 'file=fopen(TEMP_FILE_NAME,"w")', assigned value is 0<--- Assignment 'file=fopen(TEMP_FILE_NAME,"w")', assigned value is 0
    CPPUNIT_ASSERT( !!file );<--- Assuming that condition '!file' is not redundant<--- Assuming that condition '!file' is not redundant
    size_t r = fwrite( vtk_data, sizeof( vtk_data ) - 1, 1, file );<--- Null pointer dereference
    fclose( file );<--- Null pointer dereference
    CPPUNIT_ASSERT( r == 1 );

    MsqPrintError err( std::cerr );
    realMesh = new MeshImpl;
    realMesh->read_vtk( TEMP_FILE_NAME, err );
    remove( TEMP_FILE_NAME );
    ASSERT_NO_ERROR( err );
}

void TagVertexMeshTest::tearDown()
{
    delete realMesh;
    realMesh = 0;
}

void TagVertexMeshTest::test_vertex_coordinates()
{
    MsqPrintError err( std::cerr );
    TagVertexMesh tag_mesh( err, realMesh, true );
    ASSERT_NO_ERROR( err );

    std::vector< Mesh::VertexHandle > vertices;
    realMesh->get_all_vertices( vertices, err );
    ASSERT_NO_ERROR( err );

    // Check that initial position for vertex matches that of real mesh
    Mesh::VertexHandle vertex = vertices[0];
    MsqVertex get_coords;
    Vector3D orig_coords, real_coords, tag_coords;
    realMesh->vertices_get_coordinates( &vertex, &get_coords, 1, err );
    ASSERT_NO_ERROR( err );
    orig_coords = get_coords;
    tag_mesh.vertices_get_coordinates( &vertex, &get_coords, 1, err );
    ASSERT_NO_ERROR( err );
    tag_coords = get_coords;
    CPPUNIT_ASSERT_VECTORS_EQUAL( orig_coords, tag_coords, DBL_EPSILON );

    // Check that modified vertex coords show up in tag mesh but not
    // real mesh.
    Vector3D new_coords( 5, 5, 5 );
    tag_mesh.vertex_set_coordinates( vertex, new_coords, err );
    ASSERT_NO_ERROR( err );
    tag_mesh.vertices_get_coordinates( &vertex, &get_coords, 1, err );
    ASSERT_NO_ERROR( err );
    tag_coords = get_coords;
    CPPUNIT_ASSERT_VECTORS_EQUAL( new_coords, tag_coords, DBL_EPSILON );
    realMesh->vertices_get_coordinates( &vertex, &get_coords, 1, err );
    ASSERT_NO_ERROR( err );
    real_coords = get_coords;
    CPPUNIT_ASSERT_VECTORS_EQUAL( orig_coords, real_coords, DBL_EPSILON );
}

void TagVertexMeshTest::test_save_coordinates()
{
    MsqPrintError err( std::cerr );
    Vector3D new_coords( 5, 5, 5 );
    MsqVertex get_coords;

    std::vector< Mesh::VertexHandle > vertices;
    realMesh->get_all_vertices( vertices, err );
    ASSERT_NO_ERROR( err );
    Mesh::VertexHandle vertex = vertices[0];

    realMesh->vertices_get_coordinates( &vertex, &get_coords, 1, err );
    ASSERT_NO_ERROR( err );
    Vector3D orig_coords = get_coords;<--- Variable 'orig_coords' is assigned a value that is never used.

    // modify a vertex in the tag interface
    {
        TagVertexMesh tag_mesh( err, realMesh, false );
        ASSERT_NO_ERROR( err );
        tag_mesh.vertex_set_coordinates( vertex, new_coords, err );
        ASSERT_NO_ERROR( err );
    }

    // check that it exists in a new TagVertexMesh
    {
        TagVertexMesh tag_mesh( err, realMesh, false );
        ASSERT_NO_ERROR( err );
        tag_mesh.vertices_get_coordinates( &vertex, &get_coords, 1, err );
        ASSERT_NO_ERROR( err );
        CPPUNIT_ASSERT_VECTORS_EQUAL( new_coords, get_coords, DBL_EPSILON );
    }
}

void TagVertexMeshTest::test_cleanup()
{
    MsqPrintError err( std::cerr );
    Vector3D new_coords( 5, 5, 5 );
    MsqVertex get_coords;

    std::vector< Mesh::VertexHandle > vertices;
    realMesh->get_all_vertices( vertices, err );
    ASSERT_NO_ERROR( err );
    Mesh::VertexHandle vertex = vertices[0];

    realMesh->vertices_get_coordinates( &vertex, &get_coords, 1, err );
    ASSERT_NO_ERROR( err );
    Vector3D orig_coords = get_coords;

    // modify a vertex in the tag interface
    {
        TagVertexMesh tag_mesh( err, realMesh, true );
        ASSERT_NO_ERROR( err );
        tag_mesh.vertex_set_coordinates( vertex, new_coords, err );
        ASSERT_NO_ERROR( err );
    }

    // check that values were cleaned up when previous instance was destroyed
    {
        TagVertexMesh tag_mesh( err, realMesh, false );
        ASSERT_NO_ERROR( err );
        tag_mesh.vertices_get_coordinates( &vertex, &get_coords, 1, err );
        ASSERT_NO_ERROR( err );
        CPPUNIT_ASSERT_VECTORS_EQUAL( orig_coords, get_coords, DBL_EPSILON );
    }
}

void TagVertexMeshTest::test_alternate_name()
{
    MsqPrintError err( std::cerr );
    Vector3D new_coords( 5, 5, 5 );
    MsqVertex get_coords;

    std::vector< Mesh::VertexHandle > vertices;
    realMesh->get_all_vertices( vertices, err );
    ASSERT_NO_ERROR( err );
    Mesh::VertexHandle vertex = vertices[0];

    realMesh->vertices_get_coordinates( &vertex, &get_coords, 1, err );
    ASSERT_NO_ERROR( err );
    Vector3D orig_coords = get_coords;<--- Variable 'orig_coords' is assigned a value that is never used.

    // modify a vertex in the tag interface and save it
    {
        TagVertexMesh tag_mesh( err, realMesh, false, "foobar" );
        ASSERT_NO_ERROR( err );
        tag_mesh.vertex_set_coordinates( vertex, new_coords, err );
        ASSERT_NO_ERROR( err );
    }

    // verify that it is modified in the new interface
    {
        TagVertexMesh tag_mesh( err, realMesh, false, "foobar" );
        ASSERT_NO_ERROR( err );
        tag_mesh.vertices_get_coordinates( &vertex, &get_coords, 1, err );
        ASSERT_NO_ERROR( err );
        CPPUNIT_ASSERT_VECTORS_EQUAL( new_coords, get_coords, DBL_EPSILON );
    }
}

void TagVertexMeshTest::test_reference_mesh()
{
    MsqPrintError err( std::cerr );
    TagVertexMesh tag_mesh( err, realMesh, true );
    ASSERT_NO_ERROR( err );

    std::vector< Mesh::VertexHandle > vertices;
    realMesh->get_all_vertices( vertices, err );
    ASSERT_NO_ERROR( err );

    // copy real mesh coordinates into tag data in TagVertexMesh
    InstructionQueue q;
    q.add_tag_vertex_mesh( &tag_mesh, err );
    ASSERT_NO_ERROR( err );
    q.run_instructions( realMesh, err );
    ASSERT_NO_ERROR( err );

    // Check that initial position for vertex matches that of real mesh
    Mesh::VertexHandle vertex = vertices[0];
    MsqVertex get_coords;
    Vector3D orig_coords, real_coords, tag_coords;
    realMesh->vertices_get_coordinates( &vertex, &get_coords, 1, err );
    ASSERT_NO_ERROR( err );
    orig_coords = get_coords;
    tag_mesh.vertices_get_coordinates( &vertex, &get_coords, 1, err );
    ASSERT_NO_ERROR( err );
    tag_coords = get_coords;
    CPPUNIT_ASSERT_VECTORS_EQUAL( orig_coords, tag_coords, DBL_EPSILON );

    // Check that modified vertex coords show up in real mesh but not
    // tag mesh.
    realMesh->vertices_get_coordinates( &vertex, &get_coords, 1, err );
    ASSERT_NO_ERROR( err );
    orig_coords = get_coords;
    Vector3D new_coords( 5, 5, 5 );
    realMesh->vertex_set_coordinates( vertex, new_coords, err );
    ASSERT_NO_ERROR( err );
    tag_mesh.vertices_get_coordinates( &vertex, &get_coords, 1, err );
    ASSERT_NO_ERROR( err );
    tag_coords = get_coords;
    CPPUNIT_ASSERT_VECTORS_EQUAL( orig_coords, tag_coords, DBL_EPSILON );
    // restore realMesh to initial state
    realMesh->vertex_set_coordinates( vertex, orig_coords, err );
    ASSERT_NO_ERROR( err );
}