Click here to Skip to main content
15,999,861 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi folks,
I have some structs with fixed arrays. I made them internal, because I want to map them to classes with properties.

I started to write some unit tests and faced the problem that I could not access the fixed arrays in my structure. There are only methods called public void Giveth_nameOfTheField();

I tried Google put did not find a solution yet. Any suggestions or hints would be appreciated.

Thx
Andy

C#
StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
internal unsafe struct FooStruct
{
    public UInt16 A;
    public UInt16 B;
    public fixed SByte Data[20];
}

internal class FooStructMapper
{
    private FooStruct m_struct = new FooStruct();

    public void PutData(byte [] data)
    {
        // Put data into m_struct
    }
  
    public UInt16 A
    {
        get { return m_struct.A; }
        set { m_struct.A = value; }
    }

    // and so on
}


Here is the test:
C#
[TestMethod()]
[DeploymentItem("TestClient.exe")]
public void DoSomethingTest()
{
    FooStructMapper_Accessor target = new FooStructMapper_Accessor();
    object data = GetTestData(); 

    target.DoSomething(data);

    Assert.AreEqual(1, target.m_struct.A);
    Assert.AreEqual(2, target.m_struct.B);
    
    target.m_struct.Giveth_Data(); // ??? -> public void Giveth_Data();

    // expected: direct access to target.m_struct.Data
     
}


PS: Why I am doing this? I receive data from another device as byte stream. I map the byte stream to the structure to access the data fields. Then I want to make field A of the structure visible as property A of my class. Fixed arrays in the struct will be treated as "normal" arrays, too. If during a Set the new array wuld be longer than the fixed size an exception would be thrown.
Posted
Updated 29-Oct-12 1:26am
v2

1 solution

@Codeproject Admins: Sorry, I did not see the Button "I solved it myself". So here I go again:

Well, it's a kind of funny answering my own question. But I found the solution. Maybe it was a bit to easy or it is because I'm still a kind of unexperienced in unit testing.

C#
[TestMethod()]
[DeploymentItem("TestClient.exe")]
public void DoSomethingTest()
{
    FooDummy_Accessor target = new FooDummy_Accessor();
    object data = null; // GetData(); 

    target.DoSomething(data);

    Assert.AreEqual(1, target.m_struct.A);
    Assert.AreEqual(2, target.m_struct.B);

    FooStruct st = (FooStruct)target.m_struct.Target;

    sbyte[] expectedData = { 1, 2, 3, 4, 5 };
    int length = expectedData.Length;

    // CollectionAssert.AreEqual(expectedData, st.Data); does not 
    // work, because st.Data is not a collection

    unsafe
    {
        for (int i = 0; i < length; i++)
        {
            Assert.AreEqual(expectedData[i], st.Data[i]);
        }

    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900