10 Replies - 807 Views - Last Post: 10 August 2011 - 04:35 AM Rate Topic: -----

#1 steven.l.heck   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-August 11

My test has built in fails and is returning a false positive

Posted 08 August 2011 - 04:50 PM

I am trying to teach myself C#. I am working out of a book and coding in the examples. I have found this useful as I am usually able to deciphed the code before I enter it, so when I do enter it I know what is going on.
One of the things that I like is that it uses classes rather than coding everything directly into a form.
The current project in the book is a basic translator. I have entered the class and created a console app to test it.
If I am reading the code correctly the compiler should not error out, but I should get two entrys in the console telling me that what I provided as input is not valid for the translation. I am not getting an error.
Could someone tell me what I am doing wrong?
I am working with Visual Studio 2008 on a Windows 7 64 bit environment.
Attached here is my class and console app.
Thank you for any advise.

Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LanguageTranslator
{
    public class Translator
    {
        public static string TranslateHello(String input)
        {
            if (input.CompareTo("hello") == 0)
            { 
                return ("hallo");
            }
            else if (input.CompareTo ("allo") == 0)
            {
                return ("hallo");
            }
            return("");
        }
    }
}



Console App
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestLanguageTranslator
{
    class TestTranslator
    {
        static void Main(string[] args)
        {
        }
        static void TestTraslateHello()
        {
            string verifyValue;

            verifyValue = LanguageTranslator.Translator.TranslateHello ("hello");
            if (verifyValue.CompareTo("hallo") !=0)
            {
                Console.WriteLine("Test failed of Hello to Hallo");
            }
            verifyValue = LanguageTranslator.Translator.TranslateHello ("allo");
            if (verifyValue.CompareTo("hallo") !=0)
            {
                Console.WriteLine ("Test failed of Allow to Hallow");
            }
           verifyValue = LanguageTranslator.Translator.TranslateHello ("alloss");
            if (verifyValue.CompareTo("")!=0)
            {
                Console.WriteLine("Test failed to verify nontranslated word failed");
            }
            verifyValue = LanguageTranslator.Translator.TranslateHello ("  allo");
            if (verifyValue.CompareTo("hallo") !=0)
            {
                Console.WriteLine("Test failed of extra white spaces allo to hallo");                 
            }
        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: My test has built in fails and is returning a false positive

#2 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1021
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: My test has built in fails and is returning a false positive

Posted 08 August 2011 - 05:02 PM

Your Main() method (the one that is run when you execute your program) has nothing in it, so nothing runs so nothing happens. Maybe you want to put a
TestTranslateHello();
in there
Was This Post Helpful? 1
  • +
  • -

#3 steven.l.heck   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-August 11

Re: My test has built in fails and is returning a false positive

Posted 08 August 2011 - 05:08 PM

Worked perfectly, of which is to say it failed in at least one of the ways I expected. I got the expected fail due to white spaces.
However, reading the code, should it not also return a fail
            }
           verifyValue = LanguageTranslator.Translator.TranslateHello ("alloss");
            if (verifyValue.CompareTo("")!=0)
            {
                Console.WriteLine("Test failed to verify nontranslated word failed");



The string "alloss" does not match the criteria laid out in the class so I was expecting a fail there as well.

Thank you for your help. This has drove me crazy for 3 hours now.
Was This Post Helpful? 0
  • +
  • -

#4 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1021
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: My test has built in fails and is returning a false positive

Posted 09 August 2011 - 07:03 AM

Without seeing your translation code I can't tell you if it should fail or not. I would suggest changing the test to
if (String.IsNullOrEmpty(verifyValue) == false) {
    Console.WriteLine("Test failed to verify nontranslated word failed");
}

This way if the translate returns a null for non-words you catch that too.
Was This Post Helpful? 1
  • +
  • -

#5 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: My test has built in fails and is returning a false positive

Posted 09 August 2011 - 07:17 AM

Two suggestions:

1. Its unrealistic to think you can keep going with this design. You don't really plan to code hundreds of 'if...else' conditions, do you? May I suggest use of a Dictionary<string, string> ? Or a multi-dimensional array? string[,,,,] for English, German, French, Russian for example. Then you just loop through looking for a match in column one and return column x that the user has selected as the language they want translated in to.

2. If this is a homework assignment, then great; you need to do it. But if this is a project you are making up this project as a means of learning - then stop and work on a couple self-teaching books from cover to cover along with some on-line tutorials. You need to learn more of the language and the design concepts before you try building applications from scratch.


Standard resources, references and suggestions for new programmers. - Updated July 2011

Spoiler

This post has been edited by tlhIn`toq: 10 August 2011 - 07:30 AM

Was This Post Helpful? 1
  • +
  • -

#6 steven.l.heck   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-August 11

Re: My test has built in fails and is returning a false positive

Posted 09 August 2011 - 07:27 AM

Thank you for your replies. Let me immediately dispel any belief that I take any comments or criticism as anyone trying to "Be mean". I am first and foremost learning. What I am doing is self taught, so to receive criticism and or suggestions from more experienced programmers is of immense benefit.
I am working through a book, and the code that I posted is from that book.
My initial problem was due to me not reading through the next page to find where he specified to put the call into the main.
The second code segment I posted is actually one that is coded exactly according to the book, does not fail, but when I look at it the way my minds logic works it seems that it should return a false.
I realize that this is a fault in the way I am viewing the problem and am trying to understand what I am not seeing correctly.
Again, thank you for all of the incredible resources and advice.
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: My test has built in fails and is returning a false positive

Posted 09 August 2011 - 07:42 AM

One thing your book may not have shown you yet is how to place breakpoints, walk through code one line at a time, look at current values in the Locals and Autos pallet... all the things we collectively call 'debugging'. If you can actually watch the code work line by line... see the values change... see why conditions pass or fail... you start seeing what the computer sees and why it is doing what it is doing. Instead of guessing. It might be worth your time to stop, do a few debugging tutorials, then apply those new techniques back to the lessons in your book.


See FAQ #5 for debugging tutorials


FAQ (Frequently Asked Questions - Updated Aug 2011
Spoiler

This post has been edited by tlhIn`toq: 10 August 2011 - 07:30 AM

Was This Post Helpful? 1
  • +
  • -

#8 steven.l.heck   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-August 11

Re: My test has built in fails and is returning a false positive

Posted 09 August 2011 - 08:11 AM

In reading various blogs and posting I had heard of the debugging tool. You are correct, the book I am using has not mentioned it so far, and it would help. I took some programming classes around 2002, so I have the grounding in it, now it's trying to learn the .net framework and the rules specific to C# that I am doing. A lot of what I see I can understand. But reading something isn't the same as being able to do it.
You have posted a lot of resources for me to look at.
Thank you. It gives me a lot of good points to referance now and in the future.
Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5104
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: My test has built in fails and is returning a false positive

Posted 09 August 2011 - 08:16 AM

It's not supposed to "fail." Or, to put it more correctly, you're testing for the failure condition.

Walk through the steps. I suggest moving the CompareTo call outside the test and assigning it's result to a temp variable, so you can see what it is:

verifyValue = LanguageTranslator.Translator.TranslateHello("alloss");
var temp = verifyValue.CompareTo("");
if (temp != 0) {
    Console.WriteLine("Test failed to verify nontranslated word failed");
}



So, first we run through the TranslateHello method. input is "alloss". It's not "hello", and it's not "allo", so the return will be "".

Now verifyValue is "".

Next, we compare verifyValue to "". CompareTo returns 0 when equal. Therefore, temp is now 0.

Then we check temp. If temp is not equal to 0, output that line. But it is equal to 0 (since verifyValue was equal to ""), so that statement will not print.




I don't like that the book is teaching you this convoluted of a method. This isn't Java...in C#, we can compare strings with the == and != operators safely. Your tests can be simplified:

if(verifyValue != "")


Or even better, use the IsNullOrEmpty as Momerath mentioned. The point is, you don't have to jump through these hoops just to check string equality.

CompareTo is important to understand how it works, but we don't usually directly use it. The whole point is to enable operators, not to avoid them.
Was This Post Helpful? 0
  • +
  • -

#10 steven.l.heck   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-August 11

Re: My test has built in fails and is returning a false positive

Posted 09 August 2011 - 08:56 AM

Curtis

Looking at your code I think I am able to see how it would work. I'll have to try a couple of those different methods tonight and see how it comes out.
I can't speak for the validity of the book I am using as I am still trying to learn, but it was a first starting point to get me somewhere. From there I will probably go through several others before I actually attempt to write any "real" programs. Right now I'm just taking down notes for future ideas. I've got some half baked psuedo code for it, but am not willing to try it until I am comfortable that I can actually do it.
Was This Post Helpful? 0
  • +
  • -

#11 steven.l.heck   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-August 11

Re: My test has built in fails and is returning a false positive

Posted 10 August 2011 - 04:35 AM

Thank you all for the useful information. I will not post again on this thread barring some unique information, but I am keeping it for myself as there is a wealth of information here. I tested most of the suggestions that were given and will test the rest today, but I'm sure you all already know the outcome of those tests.
I take the time to go line by line through the code to try to understand what each line does.
After all of the suggestions is tested I will probably return to the original code as it is what the book is using.
Curtis, thank you for pointing out the shortcomings of this book. I will definately try a couple of others before I try calling myself even an entry level programmer. I want to know that I am at least competent, not say that I am and prove myself wrong.
Like I said at the beginning of this sermon, I will not post here again, but if anyone happens to stumble on this and has information that they believe would be useful, whether in regards to this particular code segment or in general, please email me or pm me or whatever this particular site will allow. I don't know a whole hell of a lot, but I'm smart enough to admit it, and want to learn. Books are a good start but I really believe that the best way to learn is from someone who knows more, so I welcome all input.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1