I am a Software Development Engineer in Test working for the Windows Sound team. You can contact me via email: mateer at microsoft dot com
Friend key: 28904932216450_59cd9d55374be03d8167d37c8ff4196b
The dice game Yahtzee takes thirteen turns. Each turn involves rolling a set of five dice (with two possible rerolls, the player having the option to reroll only a subset of the dice), then marking one of thirteen boxes and scoring points according to whether the numbers on the dice meet the rules of the box. After each box is marked, the game is over.
The thirteen boxes and their rules are:
There are two bonuses possible:
So a perfect Yahtzee game looks like this. On the first turn...
... then on the following turns, in no particular order (but see below:)
Now we look at the bonuses and add it all up:
So a perfect score in Yahtzee is 1480 or 1505.
Well...
... not quite. There's another rule about Yahtzees (the "Joker" rule) which I didn't tell you about yet, because it's kind of complicated. If...
... you can score this roll in any unchecked box and it automatically meets the scoring criterion (wow!) So you would get 30 points in SMALL STRAIGHT or 40 points in LARGE STRAIGHT. (Or 25 points in FULL HOUSE.)1
This gives an additional 70 or 95 points, bring the total to 1575. (It also creates additional order dependencies; the boxes for FULL HOUSE, SMALL STRAIGHT, and LARGE STRAIGHT must be checked after the ONES through SIXES box for the corresponding number is checked.)
1 Does five-of-a-kind count as a full house? I'm not sure. Mathematically it would make sense. It turns out not to matter for this calculation because of the Joker rule, but it may matter in an actual game. The official rules say "Score in this box only if the dice show three of one number and two of another", so I guess not, but I would be comfortable adopting a "house rule" which allowed a Yahtzee to count as a "native" full house. Normally a house rule should be agreed upon by all players before the start of the game, but I'm pretty sure it would be possible to convince the other players to let you score your (3 3 3 3 3) in FULL HOUSE while YAHTZEE was still open. ("You want to throw away 25 points? Really? Um, go right ahead...")
2 The lowest possible Yahtzee score is 5: get (1 1 1 1 1) and score it in Chance, then get zeros in all the other boxes.
It's sometimes difficult to explain to people what my job actually is. "I test Windows sound." "Cool. How does that work?"
A product like Windows has a lot of components that interact with each other. If everything works, the user doesn't know that most of these components even exist; everything is invisible and seamless.
Most testing involves the connection ("interface") between two components. "I test APIs." To the uninitiated, this is just a word. It sounds like "I test wakalixes." "You test what, now?"
There are two interfaces which are easier to explain. There's the software-to-hardware interface, where the driver talks to the hardware. "I test the HD Audio, USB Audio, and Bluetooth audio class drivers." "Huh?" "They make the speakers and the microphone work." "Oh, cool. So you sit around and use Skype all day?"
But the easiest of all to explain is the user interface. "I make sure that the Sound Recorder app, the volume slider, and the Sound control panel work." "Oh, that! I had this annoying problem once where..."
What does the test result for an invisible interface look like? A lot of logging. "I expected this call to succeed; it returned this HRESULT." "I poked the hardware like this and got a bluescreen." "There seems to be an infinite loop here." Lots of text.
Boring.
UI testing has logging too. But with UI testing you can also... TAKE PICTURES! A UI bug is a lot easier to understand (triage, and fix) if there's a screenshot attached (preferably with a big red rectangle highlighting the problem.)
It is therefore valuable to have an automatable utility that can take a screenshot and dump it to a file. Here's one I cribbed together from the "Capturing an Image" sample code on http://msdn.microsoft.com/en-us/library/dd183402(v=VS.85).aspx. Source and binaries attached.
This version only captures the main display, and not secondary monitors (if any.)
Pseudocode:
screen_dc = GetDC(nullptr);memory_dc = CreateCompatibleDC(screen);rect = GetClientRect(GetDesktopWindow());hbmp = CreateCompatibleBitmap(screen_dc, rect);SelectObject(memory_dc, hbmp);BitBlt(memory_dc, rect, screen_dc);bmp = GetObject(hbmp);bytes = allocate enough memorybytes = GetDIBits(screen_dc, bmp, hbmp)file = CreateFile();WriteFile(bitmap header);WriteFile(bytes);
When solving Project Euler problems I frequently need to iterate over prime numbers less than a given n. A Sieve of Eratosthenes method quickly and easily finds the small prime numbers; there are more complicated methods that find larger prime numbers, but with a couple of tweaks the Sieve of Eratosthenes can get quite high.
A naive implementation for finding the set of primes below n will:
There are a handful of simple optimizations that can be made to this naive implementation:
With these optimizations I can enumerate primes from 2 up to 5 billion (5 * 109) in about seven minutes. Source and binaries attached.
>primes 5000000000 Will enumerate primes <= 5000000000 = 5e+009 Memory for sieve: 298.023 MB Initialization complete: 983 milliseconds since start Sieving to 70711 Sieving complete: 4.70292 minutes since start Picking up the rest to 5000000000 Pickup complete: 6.12252 minutes since start Primes: 234954223 1: 2 23495423: 442876981 46990845: 920233121 70486267: 1410555607 93981689: 1909272503 117477111: 2414236949 140972533: 2924158169 164467955: 3438252577 187963377: 3955819157 211458799: 4476550979 234954221: 4999999883 Enumerating complete: 7.43683 minutes since start Freeing CPrimes object
There are more complicated sieves like the Sieve of Atkin which perform better but at the cost of being much more complex. So far I haven't had to resort to any of those.
Working on Windows, I install Windows a lot. This means a lot of my customizations have to be re-applied every time I install. To save myself some time I created a script which applies some of them. Last time I showed how to set the desktop wallpaper from a command-line app.
This time, a script to create a shortcut. The example usage creates a shortcut to Notepad and puts that in the "SendTo" folder. I find this very useful because I often need to edit text files that have non-".txt" assocations. (There are also other shortcuts I create with it.)
Here's the script:
>create-shortcut.vbs
If WScript.Arguments.Count < 2 Or WScript.Arguments.Count > 3 Then WScript.Echo "Expected two or three arguments; got " & WScript.Arguments.Count WScript.Echo "First argument is the file to create" WScript.Echo "Second is the command to link to" WScript.Echo "Third, if present, is the arguments to pass" WScript.QuitEnd If Set shell = WScript.CreateObject("WScript.Shell") Set link = shell.CreateShortcut(WScript.Arguments(0))link.TargetPath = WScript.Arguments(1) If WScript.Arguments.Count = 3 Then link.Arguments = WScript.Arguments(2)End If link.Save
>cscript create-shortcut.vbs "%appdata%\Microsoft\Windows\SendTo\Notepad.lnk" notepad.exe
Working on Windows, I find myself installing Windows a lot.
I find that I like to change a lot of the settings that Windows offers to non-default values. (That is, I'm picky.)
I have a script which automates some of these things, which I add to now and again. Some of the bits of the script are straightforward, but once in a while the tweak itself is of interest.
One of the things I love about my work setup is the many large monitors. So, one of the things I like to change is the desktop wallpaper image.
Changing the desktop wallpaper required some code, which makes it "of interest." Here's the code.
// main.cpp
#include <windows.h>#include <winuser.h>#include <stdio.h>
int _cdecl wmain(int argc, LPCWSTR argv[]) { if (1 != argc - 1) { wprintf(L"expected a single argument, not %d\n", argc); return -__LINE__; } if (!SystemParametersInfo( SPI_SETDESKWALLPAPER, 0, const_cast<LPWSTR>(argv[1]), SPIF_SENDCHANGE )) { DWORD dwErr = GetLastError(); wprintf(L"SystemParametersInfo(...) failed with error %d\n", dwErr); return -__LINE__; } wprintf(L"Setting the desktop wallpaper to %s succeeded.\n", argv[1]); return 0;}
Binaries attached.
Warning: if you pass a relative path to this tool, it won't qualify it for you, and the SystemParametersInfo call won't either - so the wallpaper you want won't be set, though all the calls will succeed. Make sure to specify a fully-qualified path.