Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# arrays
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
 
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:
[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 29-Oct-12 2:25am
Andy4112.6K
Edited 29-Oct-12 2:26am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

@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.
 
[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]);
        }
 
    }
}
  Permalink  

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

  Print Answers RSS
0 BillWoodruff 360
1 Mathew Soji 309
2 DamithSL 225
3 Afzaal Ahmad Zeeshan 202
4 Maciej Los 190
0 OriginalGriff 6,249
1 Sergey Alexandrovich Kryukov 5,853
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 3,865


Advertise | Privacy | Mobile
Web01 | 2.8.1411019.1 | Last Updated 30 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100