mirror of
https://github.com/danbulant/Cosmos
synced 2026-05-27 14:02:19 +00:00
Fixed estimate cpu speed method
Added a unit test
This commit is contained in:
parent
c4c1d10a7c
commit
39f72f9dfd
2 changed files with 87 additions and 56 deletions
19
Tests/Cosmos.Core.Tests/CPUTests.cs
Normal file
19
Tests/Cosmos.Core.Tests/CPUTests.cs
Normal file
|
|
@ -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"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -171,67 +171,79 @@ namespace Cosmos.Core
|
||||||
if (CanReadCPUID() != 0)
|
if (CanReadCPUID() != 0)
|
||||||
{
|
{
|
||||||
string s = GetCPUBrandString();
|
string s = GetCPUBrandString();
|
||||||
var _words = new List<string>();
|
return EstimateCPUSpeedFromName(s);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new NotSupportedException();
|
throw new NotSupportedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// This is only public for testing purposes
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="s"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static long EstimateCPUSpeedFromName(string s)
|
||||||
|
{
|
||||||
|
var _words = new List<string>();
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get CPU cycle speed.
|
/// Get CPU cycle speed.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue