Human Readable Binary Search

In this post, I used

  • Introduce explaining boolean variables
  • Given attention to variable naming

Please review the below code or visit https://gist.github.com/way2datta/7d32ee72289f7f5c5ea2d4068139f48d
and let me know your thoughts?

public class IntArray
{
public static int BinarySearch(int[] items, int searchTerm)
{
return SearchRecursively(items, searchTerm, 0, items.Length 1);
}
private static int SearchRecursively(int[] items, int searchTerm, int lowerIndex, int upperIndex)
{
var scannedAllItems = lowerIndex > upperIndex;
if (scannedAllItems) return 1;
var middleIndex = (lowerIndex + upperIndex) / 2;
var middleItem = items[middleIndex];
var isItemPresentAtMiddleIndex = items[middleIndex] == searchTerm;
if (isItemPresentAtMiddleIndex) return middleIndex;
var searchItemInLowerHalf = middleItem.CompareTo(searchTerm) > 0;
if (searchItemInLowerHalf) return SearchRecursively(items, searchTerm, lowerIndex, middleIndex 1);
var searchItemInUpperHalf = middleItem.CompareTo(searchTerm) < 0;
if (searchItemInUpperHalf) return SearchRecursively(items, searchTerm, middleIndex + 1, upperIndex);
return 1;
}
}
view raw BinarySearch.cs hosted with ❤ by GitHub

Witness Running Many Programs At Once from Windows Command Prompt

In this blog post, you will witness how multi-core CPU executes different processes and do context switching.

We will use the following program, this program merely takes input from the user and prints input string after some time periodically.

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Security;
using System.Threading;
internal class Program
{
[DllImport("Kernel32.dll"), SuppressUnmanagedCodeSecurity]
public static extern int GetCurrentProcessorNumber();
private static void Main()
{
var currentProcess = Process.GetCurrentProcess();
Console.WriteLine($"Please enter any string for process {currentProcess.Id} which is executed by {GetCurrentProcessorNumber()}: ");
var readline = Console.ReadLine();
var i = 0;
while (i++ < 12)
{
Console.WriteLine($"For process {currentProcess.Id} which is executed by {GetCurrentProcessorNumber()}, you have entered: {readline}");
Thread.Sleep(1000);
}
}
}
OSPlayground,cs

I presume after compiling the above file, we get the output file: ‘OSPlayground.exe’.

Now open a command prompt and navigate to the path that contains the above binary. Run the following command and witness the magic.

Command to use: start /b OSPlayground && OSPlayground

Output:

The command prompt will ask you to enter the string a couple of times since we executed the program twice.
I had inputted EARTH! for the first process from the command prompt and MARS! for another one.

The above program is executed on a multicore processor.

Local stack quick setup on my Windows 10 machine

Prerequisite:

Steps:

  • Create a file with the name docker-compose.yml.
  • Copy the following content and paste it into a docker-compose.yml.
version: "3.1"
services:
  localstack:
    image: localstack/localstack:latest
    container_name: localstack_demo
    ports:
      - "4563-4599:4563-4599"
      - "8000:8080"
    environment:
      - SERVICES=${SERVICES- }
      - DEBUG=1
      - DATA_DIR=/tmp/localstack/data
    volumes:
      - "./.localstack:/tmp/localstack"
      - "/var/run/docker.sock:/var/run/docker.sock"

  • If there is any problem due to indentation, you can download the original file from here.
  • Open PowerShell from the folder where above docker-compose.yml is kept.
  • Run docker-compose up command.
  • Builds, (re)creates, starts, and attaches to containers for all LocalStack services.
  • Now you can work with Localstack services like S3.


How to make PowerShell shorten the long path of the current directory.

Are you getting less space on the PowerShell due to the long path of the current directory?

You can increase the signal to noise ratio by hiding that long path. How?

Follow the steps:

  • Navigate to PowerShell and type $profile.
  • It will give you a path to the PowerShell profile file.
  • Navigate to the directory of that PowerShell profile file.
    It was “C:\Users\{user}\Documents\WindowsPowerShell” for me.
  • Check if the file “Microsoft.PowerShell_profile.ps1” exists on the disk.
  • If NOT then create one “Microsoft.PowerShell_profile.ps1”.
  • Open “Microsoft.PowerShell_profile.ps1” file.
  • Paste the following code in it and save.
function prompt {
  $p = Split-Path -leaf -path (Get-Location)
  "$p> "
}
  • Restart PowerShell.
  • It will show the current folder name only.

Collect human-readable code coverage report for .net core apps

In this post, I will note down steps to collect human-readable code coverage for .net core/standard apps on the developer machine.

Big Picture of Code coverage collection.

I have created a simple .NET standard class library and .net core test project (xUnit) project here.

Project structure: .NET standard class library and xUnit test project


Prerequisites

You will require the following NuGet packages to be installed on the xUnit tests project (BeingCraftsman.CoverageDemo.Tests).

  • Microsoft.NET.Test.Sdk (Preinstalled as a part of xUnit tests project)
  • xunit (Preinstalled as a part of xUnit tests project)
  • xunit.runner.visualstudio (Preinstalled as a part of xUnit tests project)
  • coverlet.msbuild
    Install it using the command:
    dotnet add package coverlet.msbuild
  • ReportGenerator
    Install it using the following two commands:
    dotnet new tool-manifest
    dotnet tool install dotnet-reportgenerator-globaltool


Steps:

  • Open Powershell and navigate to the tests project directory (In my case, BeingCraftsman.CoverageDemo.Tests).
  • Check if the command dotnet test is running all tests.
  • Collect the coverage in a folder (./CodeCoverage in my case) and mention the format opencover.
    WHY opencover?
    ReportGenerator will generate a human-readable report but for that, it requires raw code coverage file in OpenCover format.
    dotnet test /p:CollectCoverage=true /p:CoverletOutput="./CodeCoverage/" /p:CoverletOutputFormat="opencover"
  • Convert opencover format to web pages (Human readable one).
    dotnet reportgenerator "-reports:.\CodeCoverage/coverage.opencover.xml" "-targetdir:.\CodeCoverage\Web"
  • Open generated report
    start .\CodeCoverage\Web\index.htm

Human readable code coverage report giving insights about class, methods etc…


Getting tired of running all these commands one by one.
Next time, onwards, just combine them with “;” separated and execute.

dotnet test /p:CollectCoverage=true /p:CoverletOutput="./CodeCoverage/" /p:CoverletOutputFormat="opencover"; dotnet reportgenerator "-reports:.\CodeCoverage/coverage.opencover.xml" "-targetdir:.\CodeCoverage\Web"; start .\CodeCoverage\Web\index.htm



Brief description of tools/commands:

dotnet test

The dotnet test command is used to execute unit tests in a given solution. The dotnet test command builds the solution and runs a test host application for each test project in the solution.

More details: https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-test


coverlet.msbuild

Coverlet is Cross platform code coverage for .NET. How it works.

Coverlet also integrates with the build system to run code coverage after tests. In our case, MSBuild is the build system and hence we are using this package. For other build systems, you would require another package.

More details: https://github.com/coverlet-coverage/coverlet


ReportGenerator

ReportGenerator converts coverage reports generated by OpenCover, dotCover, Visual Studio, NCover, Cobertura, JaCoCo, Clover, gcov or lcov into human readable reports in various formats.

More details: https://github.com/danielpalme/ReportGenerator