Assert.AreEqual(expected, actual,

  'UnitTesting.Point.length did not return the expected value.');

}

When you run the test, the test will fail (see Figure 2-76).

Figure 2-76

Why is this so? The reason is that floating point numbers (such as Single and Double) are not stored exactly as what they have been assigned. For example, in this case, the value of 4.24264 is stored internally as 4.2426400184631348, and the result returned by the length() method is actually 4.2426406871192848. The AreEqual() method actually fails if you compare them directly.

To address this issue, the AreEqual() method supports a third parameter — delta — that specifies the maximum difference allowed for the two numbers that you are comparing. In this case, the difference between the two numbers is 0.0000066865615. And so the following code will pass the test:

Assert.AreEqual(expected, actual, 0.0000066865616,

 'UnitTesting.Point.length did not return the expected value.');

But this code will fail:

Assert.AreEqual(expected, actual, 0.0000066865615,

 'UnitTesting.Point.length did not return the expected value.');

Assert.AreEqual(expected, actual, 0.0000066865614,

 'UnitTesting.Point.length did not return the expected value.');

Although the documentation says that the delta specifies the maximum difference allowed for the two numbers, in actual testing the difference should be less than the delta for the Assert.AreEqual() method to pass. This explains why that first statement fails.

Adding Additional Test Methods

You can insert additional test methods by adding new subroutines to the PointTest.cs file and prefixing them with the [TestMethod] attribute. For example, the following test method uses the AreSame() method of the Assert class to check whether two objects are pointing to the same reference:

[TestMethod()]

public void objectTest() {

 Point point1 = new Point(4, 5);

 Point point2 = new Point() { x = 4, y = 5 };

 Point point3 = point2;

 //---Failed---

 Assert.AreSame(point1, point2, 'point1 is not the same as point2';

 //---Passed---

 Assert.AreSame(point2, point3, 'point2 is not the same as point3';

}

Figure 2-77 shows the test results.

Figure 2-77 

Summary

This chapter provided a quick overview of the common features and tools available in Visual Studio 2008. Visual Studio 2008 is highly configurable, so you'll want to take some time to familiarize yourself with the environment. If you're totally new to C#, some Visual Studio features like code refactoring and unit testing may not seem all that important to you now, but once you've gotten some C# under your belt, you'll want to take another look at those features.

When you're ready, the next chapter gets you started in writing code in C#.

Chapter 3

C# Language Foundations

The best way to get started in a new programming language is to create a simple program and then examine the various parts that compose it. With this principle in mind, you'll create a simple C# program — first using Visual Studio 2008 and then using a plain text editor.

In this chapter you build and run the HelloWorld application, using Visual Studio 2008 as well as using the command line. After that, you tackle the syntax of the C# language and all the important topics, such as:

□ C# keywords

□ Variables

□ Constants

□ Comments

□ XML documentation

□ Data types

□ Flow control

□ Loops

□ Operators

□ Preprocessor directives

Using Visual Studio 2008

The easiest way to create your first C# program is to use Visual Studio 2008. 

Editions of Visual Studio 2008

You can use any of the following editions of Visual Studio 2008 to create a C# program:

□ Visual C# 2008 Express Edition

□ Visual Studio 2008 Standard Edition

□ Visual Studio 2008 Professional Edition

□ Visual Studio 2008 Team Suite Edition

All the code samples and screen shots shown in this book were tested using Visual Studio 2008 Professional Edition.

1. Launch Visual Studio 2008.

Вы читаете C# 2008 Programmer's Reference
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату