Visual Studio 2012 Generic Class (Generics unit test)

Source: Internet
Author: User
Tags assert bool count visual studio

About unit tests, if you don't use unit tests in Visual Studio 2012, you can refer to my previous blog ————

First to share a blog post, [Visual Studio] turns on Visual Studio 2012 to create a unit test with the right mouse menu.

There are two types of generics, generic and type-constrained generics, which can be divided into unit tests for methods that contain generics, as detailed in http://msdn.microsoft.com/en-us/library/vstudio/ms243401.aspx. As you can see from this page, about generic unit testing, Microsoft Class Library (Microsoft.VisualStudio.TestTools.UnitTesting) provides a class "Genericparameterhelper" to help us write units The test code.

First look at the non-type constraint of a demo, I directly on the code

public static bool Iscollectionempty<t> (Icollection<t> 

collection)
{return
    collection = = null | | Collection. Count < 1;
}

Test code

<summary>
        ///iscollectionempty Test
        ///</summary> public
        Void Iscollectionemptytesthelper<t> ()
        {
            //Three use cases: null collection, empty collection, nulls as arguments
            icollection<t> Collection = new T[]{default (T)}; TODO: Initialize to the appropriate value
            bool expected = false;//TODO: Initialize to the appropriate value
            bool actual;
            actual = Utilitycheckdata.iscollectionempty<t> (collection);
            Assert.AreEqual (expected, actual);
    
            Collection = new t[] {};
            Assert.AreEqual (True, Utilitycheckdata.iscollectionempty<t> (collection));
    
            Assert.AreEqual (True, utilitycheckdata.iscollectionempty<t> (null));
    
        [TestMethod ()]
        public void Iscollectionemptytest ()
        {
            iscollectionemptytesthelper<genericparameterhelper> ();
        }

The test of generics is also quite simple, there is nothing to be verbose about, but if you have a type constraint, then the Genericparameterhelper class will probably no longer work.

And then I'll see what I do. A unit test code for a type-constrained generic.

Write a stack-like class that needs to be tested:

public class stacknum<t> where t:struct
    {
        list<t> array = null;
    
        Public Stacknum ()
        {
            This.array = new list<t> ();
        }
    
        public void Push (T value)
        {
            array. ADD (value);
        }
    
        Public T Pop ()
        {
            t val = array[this. LENGTH-1];
            This.array.Remove (val);
            return val;
        }
    
        public int Length
        {get
            {this.array.Count;}
    
}} Stacknum

Write a Test help class in a test project

Class Stacktesthelper {public static void lengthtest<t> () where t:struct {
            var stack = getstackinstance<t> (); Assert.AreEqual (Stack.
        Length, 0); public static void Pushtest<t> () where t:struct {var stack = getst
            Ackinstance<t> (); Stack.
            Push (Default (T)); Assert.AreEqual (Stack.
        Length, 1); public static void poptest<t> (params t[] values) where t:struct {V
            AR stack = getstackinstance<t> ();
            if (values = = null) {return;
            int pushlength = 0; foreach (T val in values) {stack.
                Push (Val); Assert.AreEqual (Stack.
            Length, ++pushlength); for (int i = stack. Length-1; I >= 0;
       i--) {         Assert.areequal<t> (Stack.
                Pop (), values[i]); Assert.AreEqual (Stack.
            Length, i);
        }} public static stacknum<t> getstackinstance<t> () where t:struct
        {return new stacknum<t> (); }} Stacktesthelper

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.