diff --git a/Tests/Cosmos.Core.Tests/CPUTests.cs b/Tests/Cosmos.Core.Tests/CPUTests.cs new file mode 100644 index 000000000..4f82aaa96 --- /dev/null +++ b/Tests/Cosmos.Core.Tests/CPUTests.cs @@ -0,0 +1,19 @@ +using NUnit.Framework; +using Cosmos.Core; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Cosmos.Core.Tests +{ + [TestFixture()] + public class CPUTests + { + [Test()] + public void EstimateCPUSpeedFromNameTest() + { + Assert.AreEqual((long)2.8e9, CPU.EstimateCPUSpeedFromName(" Intel(R) Celeron(R) CPU 2.80GHz")); + Assert.AreEqual((long)2.8e9, CPU.EstimateCPUSpeedFromName("Intel(R) Celeron(R) CPU 2.80GHz")); + } + } +} diff --git a/source/Cosmos.Core/CPU.cs b/source/Cosmos.Core/CPU.cs index 53368a982..876a8b04f 100644 --- a/source/Cosmos.Core/CPU.cs +++ b/source/Cosmos.Core/CPU.cs @@ -171,67 +171,79 @@ namespace Cosmos.Core if (CanReadCPUID() != 0) { string s = GetCPUBrandString(); - var _words = new List(); - string curr = ""; - for (int i = 0; i < s.Length; i++) - { - if (s[i] == ' ' || (byte)s[i] == 0) - { - if (curr != "") - { - _words.Add(curr); - } - curr = ""; - } - else - { - curr += s[i]; - } - } - _words.Add(curr); - string[] words = _words.ToArray(); - string[] w = new string[words.Length]; - for (int i = 0; i < words.Length; i++) // Switch order - { - w[i] = words[words.Length - i - 1]; - } - words = w; - double multiplier = 0; - double value = 0; - for (int i = 0; i < words.Length; i++) - { - if (words[i] == "MHz") - { - multiplier = 10e6; - break; - } - else if (words[i] == "GHz") - { - multiplier = 10e9; - break; - } - else if (words[i] == "THz") - { - multiplier = 10e12; - break; - } - else if (value == 0) - { - Double.TryParse(words[i], out value); - } - } - value *= multiplier; - if((long)value == 0) - { - Global.mDebugger.Send("Unable to calculate cycle speed from " + s); - throw new NotSupportedException("Unable to calculate cycle speed from " + s); - } - return (long)value; + return EstimateCPUSpeedFromName(s); } throw new NotSupportedException(); } + /// + /// This is only public for testing purposes + /// + /// + /// + public static long EstimateCPUSpeedFromName(string s) + { + var _words = new List(); + string curr = ""; + for (int i = 0; i < s.Length; i++) + { + if (s[i] == ' ' || (byte)s[i] == 0) + { + if (curr != "") + { + _words.Add(curr); + } + curr = ""; + } + else + { + curr += s[i]; + } + } + _words.Add(curr); + string[] words = _words.ToArray(); + string[] w = new string[words.Length]; + for (int i = 0; i < words.Length; i++) // Switch order + { + w[i] = words[words.Length - i - 1]; + } + words = w; + double multiplier = 0; + double value = 0; + for (int i = 0; i < words.Length; i++) + { + var word = words[i]; + var wordEnd = word.Substring(word.Length - 3, 3); + if (word == "MHz" || wordEnd == "MHz") + { + multiplier = 1e6; + } + else if (word == "GHz" || wordEnd == "GHz") + { + multiplier = 1e9; + } + else if (word == "THz" || wordEnd == "THz") + { + multiplier = 1e12; + } + if (value == 0) + { + if (Double.TryParse(word, out value) || Double.TryParse(word.Substring(0, word.Length - 3), out value)) + { + break; + } + } + } + value *= multiplier; + if ((long)value == 0) + { + Global.mDebugger.Send("Unable to calculate cycle speed from " + s); + throw new NotSupportedException("Unable to calculate cycle speed from " + s); + } + return (long)value; + } + /// /// Get CPU cycle speed. ///