PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, September 12, 2022

[FIXED] Why do two literal degree ° symbols compare equal in unit tests under Windows but not under Linux?

 September 12, 2022     .net, .net-core, character-encoding, cross-platform, unicode     No comments   

Issue

First, let me say that I understand character encodings and why the degree (°) symbol might come out as a question mark on a web page. However, I am confused why two occurrences of that literal character in code seem to not compare as equal, and one of them displays as a question mark, in a unit test but only when running under Linux.

Here's the code that should produce the degree symbol:

        public override string ToString()
        {
            var strLat = string.Format("{0} {1:D2}°{2:D2}'{3:D2}\"", IsNorth ? 'N' : 'S', Degrees, Minutes, Seconds);
            return strLat;
        }

So there it is as a literal character in my code. Now here's the unit test. This test passes when run under Windows, but not when run on my TeamCity agent under Ubuntu Linux...

    [Subject(typeof(Declination), "Conversion to sexagesimal")]
    class when_converting_a_negative_double_declination_to_sexagesimal
    {
        Because of = () => Dec = new Declination(expectedValue);
        It should_format_correctly = () => Dec.ToString().ShouldEqual("S 06°13'01\"");
        It should_have_the_correct_value = () => Dec.Value.ShouldBeCloseTo(expectedValue);
        It should_have_positive_degrees = () => Dec.Degrees.ShouldBeGreaterThanOrEqualTo(0);
        It should_have_positive_minutes = () => Dec.Minutes.ShouldBeGreaterThanOrEqualTo(0);
        It should_have_positive_seconds = () => Dec.Seconds.ShouldBeGreaterThanOrEqualTo(0);
        static Declination Dec;
        const double expectedValue = -6.21712739926718;
    }

There it is again, as a literal character, in my unit test. You'd think these would compare equal (at least, I would). But here's the result I get in TeamCity:

enter image description here

There's obviously some Unicode chicanery going on here, but I honestly can't see where! It's literally (pun intended) the same literal character in both strings. Why does this test fail?


Solution

The files must be using different encodings.

If both files contain literal ° characters and there's a mismatch, then the files must be using different encodings for that °.

Can you convert the files to the same encoding.

Alternatively, you can embed these using unicode syntax for characters/strings ("\u00b0") and stay within the safe ASCII range.



Answered By - omajid
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing