Sunday, December 09, 2018

Cross Platform .Net Core 2.0 C# Unit Testing

I wanted to make some changes to my personal .Net Core 2.0 website that lives on Heroku and is deployed via Docker. But, before doing this I wanted to setup a basic unit test project where I could test some of what I wanted to accomplish. Given that I wanted the unit tests to be cross platform I decided to use xUnit within Visual Studio Code.

Here are the steps. First, a created a folder that would hold both the objects under test as well as the unit tests.


After changing into the new unitTestsCSharp folder, I created the directory for the objects under test named ouut that stand for Objects Under Unit Tests.


From ouut I created a new project using dotnet new classlib command for the...well you guessed it...the code to be tested.


Then, back in the unitTestsCSharp directory I created a folder to hold the actual unit tests that is creatively entitled tests.


From the new tests folder I ran the dotnet new xunit command to create a new project for the unit tests.


I then opened the unitTestsCSharp folder in Visual Studio Code (VS Code). You may see dialog in VS Code informing you that some assets are missing for building and debugging and asking to add them. Select the Yes button.



From the unitTestsCSharp/ouut folder I renamed Class1.cs to MonthDateStuff.cs as I wanted to do some basic Date/Time parsing. As many of you know, when using the test-driven development (TDD) methodology, you first create a failing implementation of the MonthDateStuff class.



 using System;  
 namespace ouut  
 {  
   public class MonthDateStuff  
   {  
     public int GetMonthAsInteger()  
     {  
       throw new NotImplementedException("Sanity Test as this should fail!");  
     }  
   }  
 }  

Now we can look at the tests. Let's add the MonthDateStuff class library as a dependency to the test project using the dotnet add reference command.


Next, I changed the UnitTest1.cs name to MonthDateStuffTests.cs, added the using ouut directive, and then added a ReturnIntegerGivenValidDate method for an initial test.

 using System;  
 using Xunit;  
 using ouut;  
 namespace tests  
 {  
   public class MonthDateStuffTests  
   {  
     private ouut.MonthDateStuff _monthDateStuff;  
     public MonthDateStuffTests(){  
       _monthDateStuff = new MonthDateStuff();  
     }  
     [Fact]  
     public void ReturnIntegerGivenValidDate()  
     {  
       var result = _monthDateStuff.GetMonthAsInteger();  
       Assert.True(result.Equals(0));  
     }  
   }  
 }  

The [Fact] attribute lets the xUnit framework know that the ReturnIntegerGivenValidDate method is to be run by the test runner. From the tests folder I execute dotnet test to build the tests and the class library and then run the tests. The xUnit test runner contains the program entry point to run your tests. The dotnet test command starts the test runner using the unit test project. Here, for a sanity check, we want the test to fail to at least make sure all is setup properly.


Now, let's get the test to pass. Here is the code in the updated MonthDateStuff class.

 using System;  
 namespace ouut  
 {  
   public class MonthDateStuff  
   {  
     public int GetMonthAsInteger()  
     {  
       var dateInput = "Jan 1, 2019";  
       var theDate = DateTime.Parse(dateInput);  
       return theDate.Month;  
     }  
   }  
 }  

The test result.


Let's add more features. xUnit has other attributes that enable you to write a suite of similar tests. Here are a few:

[Theory] represents a suite of tests that execute the same code but have different input arguments.

[InlineData] attribute specifies values for those inputs.

Rather than creating several tests, use these two attributes to create a single theory. In this case, the theory is a method that tests several month integer values to validate that they are greater than zero and less than twelve:

 [Theory]  
 [InlineData(1)]  
 [InlineData(2)]  
 [InlineData(3)]  
 public void ReturnTrueGivenValidMonthInteger(int monthInt)  
 {  
  Assert.True(_monthDateStuff.IsValidMonth(monthInt));  
 }  


Note that there were 4 tests run as the theory ran three times.

Get more information about xUnit here .

Thursday, July 12, 2018

Synthesis (learning and growing) is not Easy




What does the title mean by Synthesis? Ever heard of Hegel’s Dialectic? For Georg Wilhelm Friedrich Hegel, the dialectic method of historical and philosophical progress consists of (1) a beginning idea called a thesis, (2) a competing and opposite proposal of that thesis called the antithesis, and (3) a synthesis whereby the two conflicting ideas are reconciled or “synthesized” to form a new and more advanced idea.



Isn’t that the idea of learning? To take your current ideas and put them against opposing viewpoints to achieve a better understanding?  


Here is an example of the dialectic method used in an English class:




As you can see from above, a thesis or viewpoint on a topic is not always correct. If we know that there are things to be gained from Hegel’s Dialectic, why don’t we do it more often? Simple. It is hard on us psychologically and it is mental and emotional work. When the antithesis is introduced to us from a social media post or something we have just read then there is struggle and pain to achieve synthesis.

When the antithesis is introduced to us from a social media post...then there is struggle and pain to achieve synthesis.

When your identity and thinking is on the side of or beholden to a particular thesis, considering the antithesis is difficult. You have to take time and make effort to consider antithetical viewpoints. You have to overcome preconceived ideas about those who hold the antithesis. This is not easy. No wonder we protect ourselves by surrounding ourselves with those who think like us. This is also know as an "echo chamber" where we hear the same things repeated on a continual basis.

No wonder we protect ourselves by surrounding ourselves with those who think like us.

To help in this, you must trust that once the process of synthesis is completed, or even in the painful process, you gain in various areas. First, you become more empathetic. You better understand what "they" have been feeling and thinking. Next, you learn that the topic being considered is not as simple as you originally thought. Moreover, you also better understand those complexities. However, as was previously stated, there is an element of discomfort when you hold something as true and then consider the opposing viewpoint. In short, it’s not easy but more than often, worth it.

... there is an element of discomfort when you hold something as true and then consider the opposing viewpoint. In short, it’s not easy but more than often, worth it.

Tuesday, July 10, 2018

AI and More Free Time



Technology has given us more free time. Imagine what our days would be like if we had to grow out own food, etc. AI will more likely give us more free time in the future. My question is, what will we do with it? Or another way to ask the question, "What are we doing now that we will no longer have to do which should result in more free time?"

Some things that come to my mind are drive a car, complete standard forms such as annual tax reviews, get regular medical testing, and grocery shopping.

AI driving a car is not hard to imagine. We are on the cusp of that reality.

Filling out standard forms would be something AI could do give the data that is already part of our everyday lives. Imagine the 1040EZ or 1040A forms being completed and submitted based on the information from where you work, the bills you pay, and the charities that your support.

Concerning medical tests, what if each time you urinated or defecated, your home restroom facilities would run a chemical analysis on your urine or stool and share that info with you?

Another reality that is coming soon to you is having your groceries brought to your door step. Imagine a smart fridge where what is needing replaced of your commonly purchased items automatically arrives. How would simply supplying a list to your personal AI of needed items that were at your home in less than 12 hours save you time?

Finally, if these are more possible tasks are fulfilled by AI, what would you do with that freed up time? Learn something new. Go for more walks? Spend more time with a loved one?

Tuesday, July 03, 2018

Most Advanced Yet Acceptable Ideas

In his book Hitmakers, Derek Thompson discusses an acronym, The MAYA Principle - "Most Advanced. Yet Acceptable."



This acronym was set forth by Raymond Loewy, known as the father of industrial design. Loewy's Lucky Strike cigarette package, Exxon logo, and blue nose of Air Force One are only a few of his famous designs.

Per Thompson, in his 2017 Atlantic article, "Loewy had an uncanny sense of how to make things fashionable. He believed that consumers are torn between two opposing forces: neophilia, a curiosity about new things; and neophobia, a fear of anything too new. As a result, they gravitate to products that are bold, but instantly comprehensible. Loewy called his grand theory 'Most Advanced Yet Acceptable'"

How would this work in the area of ideas? How can we better form concepts that are novel, but quickly understandable?

Tuesday, June 26, 2018

AI helping us be more ethical.



With the advent of self-driving cars, we see AI starting to make ethical decisions. How you may ask? 

These automobiles will have to decide what to do when human lives are at stake. For example, let's say a couple is in a hurry. Instead of obeying the traffic signal to not cross the street, they attempt to cross it not seeing the oncoming vehicle. Moreover, this is a self-driving car that is about to hit them. The AI calculates that it does not have enough time to come to a safe stop. In a millisecond the AI reviews the scenario. Should the ethic algorithms decide to hit the single pedestrian on the side walk instead? Hit the couple crossing the street? Or risk the life of the passengers in the car as well as those in the neighboring office building by careening into it?

Let's take this concept a step further. What if the AI knows who all the humans are that are in danger via face recognition? Also, what if the algorithms instantly calculate that one of the people in peril crossing the street has a high proclivity to criminal behavior?

Once intelligent machines start making, at least hypothetically better and more informed ethical decisions based on data, will we then slowly start to hand over our own ethical decisions to them? Why not have an AI (artificial intelligence) that has access to massive amounts of information and can process it at a thousand fold rate more then we can at least assist us in those dilemmas?

Tuesday, June 19, 2018

Less is More?

Blaise Pascal famously wrote, "I would have written a shorter letter, but I did not have the time." 


It takes effort to be brief. Speaking of that, I listen to various podcast throughout the day. One that I listen to is only one minute in length. What is interesting is that I find myself really concentrating as I listen to it. I will listen to it attentively, because it only takes a minute time, and I can afford that effort during my busy day. Moreover, I will soon have another minute to listen to it again. Secondly, I want to make sure I get that singular thought that is communicated from that one minute podcast. What I end up doing with the other podcasts that are 15 minutes to 1 hour in length is listening to them one time and even speeding up the listening process only to not hear them again. 

Why is it I pay more attention and listen more carefully to the shorter podcast? 

First, it simply takes less time. Next, because there is only a singular idea or concept contained, I want to make sure that I obtained what is so briefly being shared. 

In short, I likely spend more time listening to and then considering the shorter podcast than I do with the longer!


Thursday, May 31, 2018

Are You, Like the Emerging AI, More than the Sum of your Parts?


I recently subscribed to Medium. What I typically do is take the daily email from Medium, browse through what articles, and if I find something of interest, I save that to Instapaper to read and more commonly listen to with Instapaper's audio feature on its mobile app.

I was listening to an article, On Metamodernism by Seth Abramson. In that article he stated that, "people reduce you to your data in a way that’s soul-crushing." Immediately, what came to my mind was the Aristotelian quote, "The whole is more than the sum of its parts"

A week or so later, I was listening to the Triangulation Podcast from This Week in Technology, that featured the new book, The Fourth Age: Smart Robots, Conscious Computers, and the Future of Humanity by Byron Reese. Reese seemed optimistic and also realistic in his assessment of Artificial Intelligence (AI) and its current and potential interaction with humanity.

My typical process, after hearing of a book that I would like to read/hear is to check on my local library's web site to see if the audio or ebook is available. It did not have either. However, the Overdrive app that it uses does have the ability to recommend an audio or ebook. So, I recommend the audio book, which also puts you first in line to borrow the resource. A few days later, I get an email stating that my local library, which I love by the way, purchased the audio book and that I can start accessing it at will.

Again, my modus operandi is to run in the morning, listening to audio books and/or podcasts. So, the next morning after downloading the audio book, I am moving along listening. At the very outset Reese brings up the philosophical discussion of monism vs dualism or what is more frequently termed the mind/body problem. Before we look at these ideologies, note that where you fall on that continuum will influence your consideration of the possibility of AI becoming conscious.

To be sure, these views span a continuum. But, for discussion sake and for clarity, let's look at the two systems in contrast. Concerning consciousness,  the earliest discussions of dualist ideas are found in the writings of Plato who held that intelligence could not be identified with, or explained in terms of, their physical body. The best-known version of dualism comes from RenĂ© Descartes (1641), who held that the mind is a non-extended, non-physical substance, and separate from the physical brain. To be sure, the brain is necessary for both schools of thought, however, for the dualist consciousness or the mind "emerges" from the physical brain.

In short, a simplistic understanding is if you think that you are no more than the sum of your parts then you are a monist. Material is all that there is. Your sense of self consciousness is nothing more than a "trick" of your brain. In contrast, if you think that you are more than the sum of your physical make up, then you are a dualist and consciousness "emerges" from the brain.

So, a monist would typically respond that of course AI systems can become conscious given they possess the same physical properties that our human brain possesses. However, the dualist would say, "Hold on here, how do we know if there has been an emergent product of those physical properties that is like human consciousness or do they simply mimic the characteristics of consciousness?"

What do you "think?"