#!/usr/bin/env python
import sys

#
# From this ASN.1 grammar:
# (file DataView.asn)
#
#    DataView DEFINITIONS AUTOMATIC TAGS ::= BEGIN
#
#    T-REAL ::= REAL( -10000.0 .. 10000.0 )
#
#    END
#
# You process the ASN.1 file like this:
#
# bash$ export ASN2DATAMODEL=/opt/DMT-ToolsAndManual-Linux-090905/asn2dataModel/asn2dataModel
# bash$ mkdir output
# bash$ $ASN2DATAMODEL -o output -toPython DataView.asn
# bash$ cd output/
# bash$ make -f Makefile.python
#
# After that, you can run this script...

from DataView_asn import *

def testReal(val):
    # Create a stream buffer to host the encoded data
    d1 = DataStream(DV.T_REAL_REQUIRED_BYTES_FOR_ENCODING)
    # Create a T_REAL
    f1 = T_REAL()
    # Set the value
    f1.Set(val)
    try:
        # Encode the value of f1 into the buffer d1
        f1.Encode(d1)  
    except:
        print "Encoding failed..."
        sys.exit(1)

    binaryData = d1.GetPyString() # Get the encoded stream bytes
                                  # as a Python string. You can
                                  # pass these data over sockets,
                                  # save them to files, etc

    # Create a second stream buffer, put the encoded data
    # inside it, and decode from it.
    # Another way (one that avoids a separate DataStream)
    # is to re-use d1, calling d1.Reset (which "rewinds"
    # the stream indexes to the start, thus making it ready
    # for access by the Decoders)
    d2 = DataStream(DV.T_REAL_REQUIRED_BYTES_FOR_ENCODING)
    d2.SetFromPyString(binaryData)
    f2 = T_REAL()
    f2.Decode(d2)
    #f2.PrintAll()
    assert(abs(val - f2.Get())<1e-5)

if __name__ == "__main__":
    i = -5.0
    while i<5.0:
        testReal(i)
        i+=0.001