Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8196469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:29:19+00:00 2026-06-07T05:29:19+00:00

As a newcomer to TDD I’m stuggling with writing unit tests that deal with

  • 0

As a newcomer to TDD I’m stuggling with writing unit tests that deal with collections. For example at the moment I’m trying to come up with some test scearios to essentially test the following method

int Find(List<T> list, Predicate<T> predicate);

Where the method should return the index of the first item in the list list that matches the predicate predicate. So far the only test cases that I’ve been able to come up with have been along the lines of

  • When list contains no items – returns -1
  • When list contains 1 item that matches predicate – returns 0
  • When list contains 1 item that doesn’t match predicate – returns -1
  • When list contains 2 items both of which match predicate – return 0
  • When list contains 2 items, the first of which match predicate – return 0
  • etc…

As you can see however these test cases are both numerous and don’t satisfactorily test the actual behaviour that I actually want. The mathematician in me wants to do some sort of TDD-by-induction

  • When list contains no items – returns -1
  • When list contains N items call predicate on the first item and then recursively call Find on the remaining N-1 items

However this introduces unneccessary recursion. What sort of test cases should I be looking to write in TDD for the above method?


As an aside the method that I am trying to test really is just Find, simply for a specific collection and predicate (which I can independently write test cases for). Surely there should be a way for me to avoid having to write any of the above test cases and instead simply test that the method calls some other Find implementation (e.g. FindIndex) with the correct arguments?

Note that in any case I’d still like to know how I could unit test Find (or another method like it) even if it turns out that in this case I don’t need to.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T05:29:21+00:00Added an answer on June 7, 2026 at 5:29 am

    If find() is working, then it should return the index of the first element that matches the predicate, right?

    So you’ll need a test for the empty list case, and one for the no-matching elements case, and one for a matching element case. I would find that sufficient. In the course of TDDing find() I might write a special first-element-passes case, which I could fake easily. I would probably write:

    emptyListReturnsMinusOne()
    singlePassingElementReturnsZero()
    noPassingElementsReturnsMinusOne()
    PassingElementMidlistReturnsItsIndex()
    

    And expect that sequence would drive my correct implementation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a newcomer to TDD. What are some of the techniques you guys
I'm a complete newcomer to xslt . I'm trying to come up with a
I'm a newcomer to Qt, but I'm trying to implement what basically amounts to
As a newcomer to iOS, I found that ARC has thus far really helped
I'm a newcomer to PDO and have to say that I like it so
I'm a newcomer trying to get my feet wet with Ruby and Sinatra. I
I'm a newcomer to Rails. I want to build a simple form that determines
I'm a newcomer to Python but I understand that things should not be done
Newcomer to Objective C and trying to translate concepts and sytax I know from
I'm a newcomer to subversion. Recently, I've done some development in two different branches,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.