Preference: An Enumerated Type

Your assignment is to design, implement, and test a Preference class.

Preference is a class that models one field in an ID3 tag. It is intended to permit the user to specify the quality of an MP3 file (e.g., excellent, good, fair, poor). We need a uniform preference system that will enumerate a fixed set of value choices so that comparisons and subrange queries are possible.

Here is the public interface of the class definition to get you started.

public: Preference(int value=0); Preference(QString prefstr); /** If possible, set the host value from the given string and @return true. Otherwise return false. */ bool fromString(QString); /** @return a list of all the acceptable Preference names, ordered by value (increasing) */ virtual QStringList getNames() const;

The Assignment

A test case that shows how the Preference class must work is provided in Example 25.2. Your Preference class must pass that test.

Example 25.2. ../src/libs/filetagger/testpreference.cpp

#include "testpreference.h" #include "preference.h" #include "qstd.h" using namespace qstd; void TestPreference::test() { Preference verygood("Very Good"); Preference verygood2("Very Good"); Preference excellent("Excellent"); Preference fair("Fair"); Preference good("Good"); Preference none("None"); Preference poor("Poor"); Preference badtaste("Bad Taste"); Preference undefined("undefined"); ASSERT_EQUALS(verygood, verygood2); ASSERT_NOTEQUALS(verygood, fair); ASSERT_EQUALS(undefined, 0); ASSERT_TRUE(none > undefined); ASSERT_TRUE(poor < none); ASSERT_TRUE(badtaste < poor); ASSERT_TRUE(verygood > good); ASSERT_TRUE(good > fair); ASSERT_TRUE(fair > none); ASSERT_TRUE(fair < verygood); ASSERT_TRUE(verygood < excellent); ASSERT_EQUALS(verygood.toString(), "Very Good"); qDebug() << verygood.toString(); Preference q("notsogood"); ASSERT_EQUALS(0, (int)q); qDebug() << q.getNames().join(", "); /* Optional - Case Ignore conversions? */ // Does this print "fair is fair" or "fair is 4"? cout << "Fair is " << fair; Preference verygoodlc("very good"); ASSERT_EQUALS(verygood, verygoodlc); }

The header file is provided to you in Example 25.3 for completeness.

Example 25.3. ../src/libs/filetagger/testpreference.h

#include #include <assertequals.h> class TestPreference : public TestCase { TestPreference() : TestCase("TestPreference") {} protected: virtual void test() ; };

When the test case is run, the output should look like this.

Very Good Undefined, Bad Taste, Poor, None, Fair, Good, Very Good, Excellent ./testpreference.cpp:44: assertequalsfailed: expr var="verygood" val="6" , expr var="verygoodlc" val="0" Fair is Fair

Категории

© amp.flylib.com,