lundi 27 avril 2015

Unit Test in c# ,data from .csv file

My test function as follows:

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;

namespace ex4
{
    [TestClass]
    public class UnitTest1
    {

        public double result = 0.0;
        computation co = new computation();

        public void valuereq()
        {

            Stream myStream = null;


            var openFileDialog1 = new OpenFileDialog();
            openFileDialog1.InitialDirectory = @"C:\Users\Hassan Qamar\Desktop\share market research paper\experiment folder";

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    if ((myStream = openFileDialog1.OpenFile()) != null)
                    {
                        using (myStream)
                        {
                            string path = openFileDialog1.FileName;
                            var readstream = new StreamReader(myStream);
                            readstream.Close();
                            string[] datatoprint = File.ReadAllLines(@path);

                            result = co.LaggedCorrelation(datatoprint);
                            Console.WriteLine(result);

                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }
            }
      }




        [TestMethod]
        public void TestMethod1()
        {                  
           Assert.AreEqual(9.8,result,0.5);                  
        }

    }
}

I am extracting value from .csv file and passing it for computation. The expected result should be 9.6 approx. But while testing it showing 0 in assert function.

Computation class as follows:

using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace ex4
{
    public class computation
    {
        Form1 f = new Form1();
        public double output;

        public double LaggedCorrelation(string[] datatoprint)
        {
            List<double> laggedCorrelation = new List<double>();
            int cond = 0;

            double n = datatoprint.Length - 1;//removing header row
            double xsum = 0.0, ysum = 0.0, x = 0.0, y = 0.0, xy = 0.0, xsquare = 0.0, ysquare = 0.0;
            //  while (cond < 2)
            //  {
            //   double output = 0.0; 
            double numerator = 0.0, denominator = 0.0;


            foreach (var l in datatoprint.Skip(1))
            {
                string[] s = l.Split(',');
                x = Convert.ToDouble(s[cond]); y = Convert.ToDouble(s[cond +1]);
                xsum += x; ysum += y;
                xy += x * y;
                xsquare += x * x; ysquare += y * y;
            }

            cond++;

            numerator = (n * (xy)) - (xsum * ysum);
            denominator = (Math.Sqrt(n * xsquare - xsum * xsum)) * (Math.Sqrt(n * ysquare - ysum * ysum));
            output = numerator / denominator;
            laggedCorrelation.Add(output);



            return output;
        }
    }
}

Computation function give the lagged correlation between 2 given stock.when I work without testing I get the value as required otherwise in test function. Output remain 0.

Aucun commentaire:

Enregistrer un commentaire