
//==== Size of a file in bytes, or -1 if it can not be opened ====//
int FileSize( const string & in fname )
{
    file f;
    if ( f.open( fname, "r" ) < 0 )
    {
        return -1;
    }
    int sz = f.getSize();
    f.close();
    return sz;
}

int main()
{
    int failure = 0;

    Print( string( "Begin Facet Export Test" ) );
    Print( string( "" ) );
    failure += TestFacet();
    Print( string( "End Facet Export Test" ) );

    //==== Check For API Errors ====//
    while ( GetNumTotalErrors() > 0 )
    {
        ErrorObj err = PopLastError();
        Print( err.GetErrorString() );
        failure++;
    }

    return failure;
}

int TestFacet()
{
    int failure = 0;

    Print( string( "--> Generating Geometries" ) );
    //Print( string( "" ) );

    //==== Add Pod and set some parameters ====//
    string pod_id = AddGeom( "POD", "" );

    //==== Add SubSurfaces and set some parameters ====/
    string subsurf_ellipse_id = AddSubSurf( pod_id, SS_ELLIPSE, 0 );

    string subsurf_rectangle_id = AddSubSurf( pod_id, SS_RECTANGLE, 0 );

    SetParmVal( FindParm( subsurf_rectangle_id, "Center_U", "SS_Rectangle" ), 0.6 );

    Update();

    //==== CFDMesh Method Facet Export =====//
    string cfd_file = "TestCFDMeshFacet.facet";
    SetComputationFileName( CFD_FACET_TYPE, cfd_file );

    Print( string( "---> Computing CFDMesh..." ), false );

    ComputeCFDMesh( SET_ALL, SET_NONE, CFD_FACET_TYPE );

    Print( string( "COMPLETE" ) );

    //==== MeshGeom Method Facet Export =====//
    string mesh_file = "TestMeshGeomFacet.facet";

    Print( string( "---> Computing MeshGeom..." ), false );

    ExportFile( mesh_file, SET_ALL, EXPORT_FACET );

    Print( string( "COMPLETE \n" ) );

    //==== Both routes should have written a facet file with content in it ====//
    int cfd_size = FileSize( cfd_file );
    if ( cfd_size <= 0 )
    {
        Print( "FAIL CFDMesh facet export produced no file, size " + cfd_size );
        failure++;
    }

    int mesh_size = FileSize( mesh_file );
    if ( mesh_size <= 0 )
    {
        Print( "FAIL MeshGeom facet export produced no file, size " + mesh_size );
        failure++;
    }

    Print( "   " + cfd_file + " " + cfd_size + " bytes, " + mesh_file + " " + mesh_size + " bytes" );

    return failure;
}
